Results 1 to 8 of 8

Thread: Help sorting listbox in Ascending order(listbox holds only numbers)

  1. #1
    Frenzied Member
    Join Date
    Apr 05
    Posts
    1,907

    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

  2. #2
    Frenzied Member
    Join Date
    Aug 11
    Location
    B.C., Canada
    Posts
    1,946

    Re: Help sorting listbox in Ascending order(listbox holds only numbers)

    Did not read post properly so i edited post sorry!

    You need to use some kind of sort algorithm theres alot... Cant a listbox sorte? The sorted property
    Last edited by Max187Boucher; Oct 27th, 2012 at 11:39 PM.

  3. #3
    Frenzied Member SamOscarBrown's Avatar
    Join Date
    Aug 12
    Location
    NC, USA
    Posts
    1,638

    Re: Help sorting listbox in Ascending order(listbox holds only numbers)

    Sorry-posted an answer, but because I'm on my iPad, don't know if correct. So deleted it

  4. #4
    Hyperactive Member
    Join Date
    Mar 08
    Posts
    500

    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.

  5. #5
    Frenzied Member
    Join Date
    Apr 05
    Posts
    1,907

    Re: Help sorting listbox in Ascending order(listbox holds only numbers)

    Quote Originally Posted by SamOscarBrown View Post
    Sorry-posted an answer, but because I'm on my iPad, don't know if correct. So deleted it
    Thanks for reply but i dont see any code !

  6. #6
    Frenzied Member
    Join Date
    Apr 05
    Posts
    1,907

    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 ?


    Quote Originally Posted by Magic Ink View Post
    Try;
    List1.AddItem Right$("0000" & strItem,5)
    or
    List1.AddItem Right$(Space$(4) & strItem,5)

  7. #7
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,978

    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.

  8. #8
    PowerPoster
    Join Date
    Feb 06
    Posts
    8,669

    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).
    Attached Files Attached Files

Posting Permissions

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