Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting tab-delimited items in a ListBox 2

Status
Not open for further replies.

MikeCox

Programmer
Jun 4, 2001
340
US
Hello.

I have a ListBox that displays information from my database, and I'm simulating columns using tabs between values. All the values are retrieved directly from fields in the database save one, which is a calculation of two other values (a percentage ratio). I can easily sort the other fields using the database engine, but I'm having trouble sorting by the calculated field (which, not surprisingly, is what my users want it to do).

I found and began using the BubbleSort algorithm, which does a terrific job sorting the percentage values, but now I'm lost as to how to sort the ListItems() strings into the same order. In other words, I have an array of Percentages(), and an array of ListItems(). Once the Percentages() array is sorted, how can I populate the ListBox with ListItems() using the sorted Percentages() array?

BTW: BubbleSort (and all others I've found) doesn't work on arrays of UDT's, so that shot that idea down, too :-(

Thanks for any ideas.

-Mike
Difference between a madman and a genius:
A madman uses his genius destructively,
A genius uses his madness constructively.
 
BYPASS IF LIST IS EMPTY ELSE KABOOM!!!!!
Build an array of indexes to be a minor key.
Code:
Dim aryIdx() as long
Dim I as long
Redim aryIDX(listbox.ListCount - 1)
For I = 0 to Ubound(aryIDX)
    aryIdx(I) = I
Next
In the sort you need to "swap" the aryIdx items just as you do the sort item. When finished, aryIdx has the sorted Indexes (indices) of the original items.
Pull the strings out of the list box and save.
Code:
Dim arySave() as string
With listbox
    Redim arySave(Ubound(aryIdx))
    For I = 0 to Ubound(aryIDX)
        arySave)I) = listbox.List(I)
    Next
    .Clear
    For I = 0 to Ubound(aryIdx)
        .add arySave(aryIDX(I)) ' indirect subscript
    Next
End With
probably syntax errors etc but it is a map.
 
Cool, I think I origionally thought that would be necessary, but then I got to thinking I have the listbox.List(index) to work with, I think that's where my confusion was at.

Thanks John!
Difference between a madman and a genius:
A madman uses his genius destructively,
A genius uses his madness constructively.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top