Listbox multiple column sort methods

Give a listbox with contents that will contain unsorted names and then a volume value and then within the volume, Is this something that can be done using the existing Listbox sort options are am I looking at either splitting the content into arrays or hidden listboxes and then re-coagulate the results?

Name Vol Offset Tim 1 2311 Norm 1 342 Alan 1 1102 Bill 2 121 Jeff 3 11213 Roger 2 10221 Andy 1 23141 Gil 3 123

The volume should be the primary sort and the offset should be the sub-sort.

Pretty sure you’re gonna have to roll your own there.

Implement the CompareRows event anyway you want.

I think you can do it in listbox compare. Somewhere I’ve done it with two columns so I 3 columns would be one more step. I’ll see if I can find it.

I think I found the method by googling “xojo or realbasic” two column sort.

Henry

consider vol and offset as parts of a floating point value

.

you’ll need to normalize the decimals so they all have the same # of digits but once you do you can sort them numerically

I would try to add a hidden column and concat Vol and Offset and than you can sort only by 1 column
what do you think ?

[quote=483454:@Norman Palardy]consider vol and offset as parts of a floating point value

.

you’ll need to normalize the decimals so they all have the same # of digits but once you do you can sort them numerically[/quote]

[quote=483486:@Johann Kienbrandt]I would try to add a hidden column and concat Vol and Offset and than you can sort only by 1 column
what do you think ?[/quote]
I actually just did this before checking back in here:

lbRestoreSelected.Cell(lbRestoreSelected.LastIndex, 7) = Format(Val("&h" + localVol), "000") + "." + _ Format(Val(localqfa), "0000000000")
Where column 7 is hidden.

Great minds, and all that!

[quote=483454:@Norman Palardy]consider vol and offset as parts of a floating point value

.

you’ll need to normalize the decimals so they all have the same # of digits but once you do you can sort them numerically[/quote]

And just for the sake of keeping the code clean, I’d put the floating point value for each row into a RowTag, then compare them in the CompareRows event.

Since you need to sort by 2 or more criteria, I would populate an SQLite Work table, sort it, and rebuild the Listbox. Just a few easy to understand SQL Statements.

In memory SQLite…

If you have a method to populate the listbox from a database, I tend to just call that method from the HeaderPressed event handler. Build the ORDER BY based on the column.

Didn’t know that - In memory SQLite would work like lightning and I presume you can kill the memory space if it is never required again in the app. Thanks for embellishing my knowledge. Have you any examples on how you would create an in memory SQLite Database ?

I have a vague recollection that the old VB6 included information in a column row that allowed you to store a cell “display” as well as how that cell value was to be used in a column sort (value) sort of way.

I could be wrong, it’s been so long, but I too have been thinking about this lately for a project. The best example I have is byte values for file length:

5 bytes is just 5 bytes (5)
999 bytes = 999 bytes (999)
but then we start getting tricky with
1024 bytes = 1kb (and stored value of 1024)
and so on.

I like what George said about rebuilding from a new query, that does sound good (If it’s in fact built from a database).

If you were to implement it in code which would be the better way:

  • Subclass the cell to include some sort of sort value
  • use hidden columns to achieve the same end
  • implement your own sorting method (as has been exampled above)

I think 1 and 2 are virtually the same, but would there be any benefit one way over any other?

This is all you need for an in-memory SQLite database::

Var dbh As SQLiteDatabase

dbh = new SQLiteDatabase
dbh.Connect ()

Thanks Tim for the SQLite Code.
Another cute idea for sorting data is to add your data to a hidden listbox which has Sorted Mode set to YES. (Not sure if this works in XOJO - this is from VB6). You add your data row by row to the hidden listbox with a row number appended to the data. (using a delimiter you can split off the row number later and use as a pointer to the original list).
Eg, Listbox addrow “data from row 1;1”, Listbox addrow “Moredata from row 2;2” , etc
You then just read the data from the hidden list box (which is now automatically sorted) and process the data in sorted order using the appended row numbers.

I was originally loading the listbox from an SQLite database, and then doing the sorting using the sort event and HeaderPressed event. Then I realised that with my particular column contents, it would be just as easy to make a note of which header was pressed, and use that column number to generate an ORDER BY for the method that does the loading. So I scrap the sorting event and just reload the data from within the HeaderrPressed event. So I let SQLite do the sorting.

My impression is that after a go or two, the database is in the cache (at least on macOS) and the whole process becomes quite quick.