ListBox example of sorting for more than a column

Is there any example to implement the sorting of a ListBox for more than a column?
When clicking the column header the list should be ordered by this column and equal values of this column be ordered on other column also and so on

I tried the compare rows but no success (probably I do it wrong)

From where your data comes ?

If from a Data Base, do the sort at the RecordSer asking time. Else, wait for a better answer, sorry.

It is one thing when your program logic knows what should be the secondary sorts when the primary sort column has duplicate values. And it is another if you want the user to dynamically select the secondary sort column(s). It isn’t clear from your statement above if the user or the program gets to decide the secondary sort column(s).

If it is the user, then you will need to implement some UI as by default the ListBox only supports a UI to determine the primary sort column.

If your program logic can determine the secondary sort column(s), then CompareRows() is your friend. In doing a quick search for an existing forum sample, I came across a thread that somehow I think you’ll already find familiar here but that thread leads me to believe what you are looking for is a method to let the user pick multiple sort columns.

To my knowledge, there is no existing generic extension to add the functionality to let the user specify secondary sort columns. There are at least a couple of alternatives to how you can code one, like:

  • Code a window where user gets to define the list of columns (ala Excel style custom sorts)
  • Implement a contextual menu on a header click and give options for 1st, 2nd, 3rd, etc sort priority (store in properties)

Once you do have the sort priorities, you implement using CompareRows() as shown in that thread (as one example). If populating the ListBox as opposed to acting on user clicks of column headers, you may also want to pre-sort your data and add rows already in the right sequence. This is especially easy if you are using a database and can use an SQL SELECT with a proper ORDER BY clause.

I forgot the part Douglas is talking above.

Addition:
If you want to display sort on always the same columns, create an in memory db, store your data there, and import them in your Listbox sorted by the columns you want using the DB instructions.

If this is a fixed sort. Else, as Douglas already said, you have to ask the user to define what columns to sort (primary, secondary, etc.) and then in code do the job at db read time.

Ask if not clear enough.

I had the same problem sometime ago, and finished doing a new set with SQL SELECT and ORDER by, but I believed that maybe there is a better approach.

Since we do not know the data source, it is a bit difficult to give advice(s) or clue(s). IMHO.

the data source is an SQLLite database, the list used to have about 2.000 records, then the user has to be able to select a column to sort (and doing this the list must be ordered but this and two other fixed columns).
The first approach is a query to the database to get the same set that is already in the list and ORDER BY the two others fields.

I though that maybe there is a better approach, as the set is already in the list just need to reorder it.

Why a better approach ?

As I wrote earlier, create an in Memory dab (for speed) copy the data there, create a new RecordSet and sort the data at will, then copy the data in the Listbox. With 2,000 records (unknow # of COlumns), the speed is certaily fast enough to not be noticeable.

If the speed is not enough, you can hide the Listbox while importing the data in the Listbox and once done, set the Listbox back to visible.

Is this OK for you ?

Thanks all.
I just asked to see if there is a better way to reorder a in memory set, to have to create another set already ordered.

[quote=402576:@Enric Herrera]the data source is an SQLLite database, the list used to have about 2.000 records, then the user has to be able to select a column to sort (and doing this the list must be ordered but this and two other fixed columns).
The first approach is a query to the database to get the same set that is already in the list and ORDER BY the two others fields.

I though that maybe there is a better approach, as the set is already in the list just need to reorder it.[/quote]

By “two other fixed columns” do you mean that regardless of the top priority column selected by the user (by clicking a column heading), that equal values within that column should always be ordered by a specific column for the secondary sort and another specific column for the third priority?

If so, then this can be accomplished easily with your own CompareRows() handler. You’d just first compare the contents of the column it names in the method arguments. This would be the primary sort column selected by the user. If they are not equal, you return 1 or -1 as shown in the docs for CompareRows . But when the primary column is equal, instead of returning 0 you then compare your fixed secondary column number and again return 1 or -1 if not equal. If they are equal, instead of returning 0 you then compare your fixed third column number. At that point you return 1, -1, or 0 (if they are equal as well).

Then the user can click on any column and without reloading the data, Xojo will just sort the rows according to your CompareRows() logic. What is more complicated here is if the column(s) to be used as secondary sort values must be user selectable too. If so you must provide the UI so your CompareRows() method knows the priority of the columns to be used in the sort.