Results 1 to 9 of 9

Thread: [RESOLVED] List Sorting

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Resolved [RESOLVED] List Sorting

    ok, i have done this before but lost the code . does anyone know a way where i can sort a listbox that already has random numbers in it? or does anyone know a way where i can add a listbox and make my incoming numbers be sorted as you go?

    I DO NOT WANT THE LISTBOX SORT=TRUE code because that doesn't really do anything that i want it to do.

    Thanks again for the posts you post. This would be GREATLY appretiated

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: List Sorting

    Welcome to the Forum.

    If you want your ListBox to be sorted at runtime then this might help
    VB Code:
    1. Public Function sortListBox(thisList As ListBox)
    2.     Dim firstCounter As Long, temp As String, secondCounter
    3.     'loop from first to last item
    4.     ' this will start sorting the items from the second item.
    5.     For firstCounter = 1 To thisList.ListCount - 1
    6.         'loop from the firstCounter+1 item to last item
    7.         For secondCounter = firstCounter + 1 To thisList.ListCount - 1
    8.             'check if the value is greater
    9.             If thisList.List(firstCounter) > thisList.List(secondCounter) Then
    10.                 'swap the items here
    11.                 '----------------------------------------------
    12.                 temp = thisList.List(firstCounter)
    13.                 thisList.List(firstCounter) = thisList.List(secondCounter)
    14.                 thisList.List(secondCounter) = temp
    15.                 '----------------------------------------------
    16.             End If
    17.         Next
    18.     Next
    19. End Function
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: List Sorting

    or add another listbox (list2) and set .Visible = False and .Sorted = True.

    Then to add an item to your listbox do:
    VB Code:
    1. Private Sub AddToList(ByVal lNum As Long)
    2.     List2.AddItem Right$(String(10, "0") & lNum, 10)
    3.     List1.AddItem lNum, List2.NewIndex
    4. End Sub

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Re: List Sorting

    thanks guys. im about to try both of your suggestions and see which one works best. but ali, where would i place that code?

    Edit:

    I just finished trying your code ali. This was my before and after.

    Before Numbers in List Box:
    1
    12
    20
    4
    78
    40
    98

    After I did your code:
    1
    12
    20
    4
    40
    78
    98

    is it supposed to be like this or am i doing something wrong?
    Last edited by GRPsuper9; Jun 7th, 2006 at 11:00 AM.

  5. #5
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: List Sorting

    Whenever you want to sort the Listbox, I guess at the time of inserting the values.

    And a little correction to the If condition would be
    VB Code:
    1. If Val(thisList.List(firstCounter)) > Val(thisList.List(secondCounter)) Then

    The actual code was written to sort alphabets only.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Re: List Sorting

    bush, i just tried ur code as well. this is what i had in my code

    VB Code:
    1. Private Sub AddToList(ByVal lNum As Long)
    2.     List2.AddItem Left$(String(10, "0") & lNum, 10)
    3.     List1.AddItem lNum, List2.NewIndex
    4. End Sub
    5.  
    6. Private Sub Form_Load()
    7. AddToList "1"
    8. AddToList "20"
    9. AddToList "10"
    10. AddToList "1001"
    11. End Sub

    but when it sorted it, it sorted it in this order.
    10
    1001
    20
    1

  7. #7

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Re: List Sorting

    ali! thanks a ton man! ur code seemed to help! woah. ur the best .
    okay, for other people who are searching how to do this to, ill post the code i have that ali gave me, plus ill post how to sort it with his code. so then if anyone searches this, they can get it right away. here is the code :

    VB Code:
    1. Public Function sortListBox(thisList As ListBox)
    2.     Dim firstCounter As Long, temp As String, secondCounter
    3.     'loop from first to last item
    4.     ' this will start sorting the items from the second item.
    5.     For firstCounter = 1 To thisList.ListCount - 1
    6.         'loop from the firstCounter+1 item to last item
    7.         For secondCounter = firstCounter + 1 To thisList.ListCount - 1
    8.             'check if the value is greater
    9.             If Val(thisList.List(firstCounter)) > Val(thisList.List(secondCounter)) Then
    10.                 'swap the items here
    11.                 '----------------------------------------------
    12.                 temp = thisList.List(firstCounter)
    13.                 thisList.List(firstCounter) = thisList.List(secondCounter)
    14.                 thisList.List(secondCounter) = temp
    15.                 '----------------------------------------------
    16.             End If
    17.         Next
    18.     Next
    19. End Function
    20.  
    21.  
    22. Private Sub Form_Load()
    23. List1.AddItem "1"
    24. List1.AddItem "2"
    25. List1.AddItem "10"
    26. List1.AddItem "45"
    27. List1.AddItem "7"
    28. List1.AddItem "70"
    29. List1.AddItem "46"
    30. sortListBox List1
    31. End Sub

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Re: List Sorting

    btw, thanks bush for trying to help aswell

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width