Results 1 to 8 of 8

Thread: listbox sort by length ?

  1. #1

    Thread Starter
    Fanatic Member kevin_sauerwald's Avatar
    Join Date
    Feb 2002
    Location
    outside Philly
    Posts
    516

    listbox sort by length ?

    I am using a list box to grab a bunch of names that will be
    use as a replace list... issue is if you have something like
    grab_proc and grap_proc_name and you do a replace with
    with lets say g_grab_proc you end up getting
    a double hit on grab_proc_name..

    so if I can sort the list to longest to shortest I think it will fix
    it.... and I cant just name the replace use a space afterwards to
    fix it..

    any ideas ??

  2. #2
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    So then, do you just want to sort your ListBox in terms of string length instead of ASCII sorting (which is built into the control)?

  3. #3

    Thread Starter
    Fanatic Member kevin_sauerwald's Avatar
    Join Date
    Feb 2002
    Location
    outside Philly
    Posts
    516
    yep.. thats it... Not by A-Z or what not...

    I could write code to do it.. just wondering if there
    was some way to change the sort criteria...

    or someone had quick code snipit to do it..

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    Here is a quick sample for you, Kevin:
    VB Code:
    1. Public Sub SortByLength(lst As ListBox)
    2. '=======================================
    3. Dim i&, j&, tmpValue
    4. Dim arTemp() As String
    5.  
    6.     Debug.Print "Start Time: " & Format(Now, "hh:nn:ss")
    7.    
    8.     ReDim arTemp(lst.ListCount - 1)
    9.     For i = 0 To lst.ListCount - 1
    10.         arTemp(i) = lst.List(i)
    11.     Next i
    12.     lst.Clear
    13.     For i = 0 To UBound(arTemp)
    14.         For j = i To UBound(arTemp)
    15.             If Len(arTemp(i)) > Len(arTemp(j)) Then
    16.                 tmpValue = arTemp(j)
    17.                 arTemp(j) = arTemp(i)
    18.                 arTemp(i) = tmpValue
    19.             End If
    20.         Next
    21.         lst.AddItem arTemp(i)
    22.     Next
    23.    
    24.     Debug.Print "End Time: " & Format(Now, "hh:nn:ss")
    25.  
    26. End Sub

  5. #5
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339
    Debug.Print "Start Time: " & Format(Now, "hh:nn:ss")

    What does Now do?
    the rest is easy, I aint never seen that Now used before
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    returns system time

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    Originally posted by dglienna
    returns system time
    To be exact: System Date and Time

  8. #8
    Lively Member
    Join Date
    Jun 2004
    Posts
    74
    I have used a recursive QuickSort (which you can probably do a Goolge on) for sorting. I pass a variable through hte Sort function to a CompareFunction that lets you have variable methods of Sorting/Comparing without the need for a different sort for each.

    1 = Sort by Length,
    2 = Sort Alphabetically
    3 = ...

    HTH

    Hume

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