|
-
Jun 7th, 2006, 10:25 AM
#1
Thread Starter
Fanatic Member
[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
-
Jun 7th, 2006, 10:30 AM
#2
Re: List Sorting
Welcome to the Forum. 
If you want your ListBox to be sorted at runtime then this might help
VB Code:
Public Function sortListBox(thisList As ListBox)
Dim firstCounter As Long, temp As String, secondCounter
'loop from first to last item
' this will start sorting the items from the second item.
For firstCounter = 1 To thisList.ListCount - 1
'loop from the firstCounter+1 item to last item
For secondCounter = firstCounter + 1 To thisList.ListCount - 1
'check if the value is greater
If thisList.List(firstCounter) > thisList.List(secondCounter) Then
'swap the items here
'----------------------------------------------
temp = thisList.List(firstCounter)
thisList.List(firstCounter) = thisList.List(secondCounter)
thisList.List(secondCounter) = temp
'----------------------------------------------
End If
Next
Next
End Function
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Jun 7th, 2006, 10:48 AM
#3
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:
Private Sub AddToList(ByVal lNum As Long)
List2.AddItem Right$(String(10, "0") & lNum, 10)
List1.AddItem lNum, List2.NewIndex
End Sub
Last edited by bushmobile; Jun 7th, 2006 at 11:09 AM.
-
Jun 7th, 2006, 10:56 AM
#4
Thread Starter
Fanatic Member
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.
-
Jun 7th, 2006, 11:01 AM
#5
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:
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
-
Jun 7th, 2006, 11:05 AM
#6
Thread Starter
Fanatic Member
Re: List Sorting
bush, i just tried ur code as well. this is what i had in my code
VB Code:
Private Sub AddToList(ByVal lNum As Long)
List2.AddItem Left$(String(10, "0") & lNum, 10)
List1.AddItem lNum, List2.NewIndex
End Sub
Private Sub Form_Load()
AddToList "1"
AddToList "20"
AddToList "10"
AddToList "1001"
End Sub
but when it sorted it, it sorted it in this order.
10
1001
20
1
-
Jun 7th, 2006, 11:08 AM
#7
Re: List Sorting
my bad 
change Left$ to Right$
-
Jun 7th, 2006, 11:10 AM
#8
Thread Starter
Fanatic Member
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:
Public Function sortListBox(thisList As ListBox)
Dim firstCounter As Long, temp As String, secondCounter
'loop from first to last item
' this will start sorting the items from the second item.
For firstCounter = 1 To thisList.ListCount - 1
'loop from the firstCounter+1 item to last item
For secondCounter = firstCounter + 1 To thisList.ListCount - 1
'check if the value is greater
If Val(thisList.List(firstCounter)) > Val(thisList.List(secondCounter)) Then
'swap the items here
'----------------------------------------------
temp = thisList.List(firstCounter)
thisList.List(firstCounter) = thisList.List(secondCounter)
thisList.List(secondCounter) = temp
'----------------------------------------------
End If
Next
Next
End Function
Private Sub Form_Load()
List1.AddItem "1"
List1.AddItem "2"
List1.AddItem "10"
List1.AddItem "45"
List1.AddItem "7"
List1.AddItem "70"
List1.AddItem "46"
sortListBox List1
End Sub
-
Jun 7th, 2006, 11:11 AM
#9
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|