Help sorting listbox in Ascending order(listbox holds only numbers)
Hi all . i am trying to sort a listbox while filling the listbox in ascending order. My current code sorts like this :
1013
1034
716
717
829
830
but i want to sort them like this:
716
717
829
830
1013
1034
could you guys help me sort like above.Thanks
Code:
Private Sub Command2_Click()
Dim hrefSPLIT() As String
Dim strItem As Integer
''''''''''''
Dim doc As HTMLDocument
Dim aHref As HTMLAnchorElement
Set doc = WebBrowser1.Document
For Each aHref In WebBrowser1.Document.All.tags("A")
If InStr(aHref.href, "http://www.somesite.com/en/programs/") Then
aHref.href = Replace(aHref.href, "http://www.somesite.com/en/programs/", "")
hrefSPLIT = Split(aHref.href, "/")
Text6.Text = hrefSPLIT(0)
aHref.href = Replace(aHref.href, hrefSPLIT(0) & "/", "")
hrefSPLIT = Split(aHref.href, "/")
Text11.Text = Val(hrefSPLIT(0))
strItem = Text11.Text
List1.AddItem CInt(strItem)
End If
Next aHref
End Sub
Re: Help sorting listbox in Ascending order(listbox holds only numbers)
Try;
List1.AddItem Right$("0000" & strItem,5)
or
List1.AddItem Right$(Space$(4) & strItem,5)
Correction that should have been
Try;
List1.AddItem Right$("0000" & strItem,4)
or
List1.AddItem Right$(Space$(4) & strItem,4)
to handle numbers up to 4 characters long.
To handle longer numbers use eg. Right$(Space$(5) & strItem,5) etc...
Last edited by Magic Ink; Oct 28th, 2012 at 11:43 AM.
Re: Help sorting listbox in Ascending order(listbox holds only numbers)
Thanks magic. i tried the second one and it worked well! does it sort only 4 digit numbers ?because may be in future i get numbers which are 5 digits or more so same code will work or should i change anything ?
Originally Posted by Magic Ink
Try;
List1.AddItem Right$("0000" & strItem,5)
or
List1.AddItem Right$(Space$(4) & strItem,5)
Re: Help sorting listbox in Ascending order(listbox holds only numbers)
A list box does an alpha sort so if you want numbers to be sorted in numeric order they must be left padded to make them all the same length. It doesn't matter it tey are 4 characters or 20 so long as they are all the same. The reason is that an alpha sort starts by looking at the first character 1 comes before 2 so in an alpha sort 1111111111 also comes before 2 as would 199999999999. Alpha sort only looks at the second character if the first character is the same then will do the same thing with the second character and continue until it either finds one that is different or reaches the end.
Re: Help sorting listbox in Ascending order(listbox holds only numbers)
WinXP and later have shlwapi.dll 5.5 and greater, which has a spiffy entrypoint called StrCmpLogicalW() that can help here.
Typical VB6 Declare signature:
Code:
Private Declare Function StrCmpLogicalW Lib "shlwapi" ( _
ByVal lpsz1 As Long, _
ByVal lpsz2 As Long) As CmpLogicalResults
You can use this to implement a full blown sort, or when your lists of data tend to consist of 1000 items or less you can just use an "insert in order" technique that is easier to get right (or debug if you get it wrong).
See the demo (attached) which does what you seem to be asking for, using an insertion approach. You can shave some time off it by a few changes if you aren't using ListBox.ItemData to hold anything (see code comments).