Sort 2-dimensional array

Before I reinvent the wheel: does anyone have a method to sort a 2-dimensional array? I have some database metadata where I can’t use “order by” and need to sort the data before shoving it into a listbox.

I did some Googling but I’m lazy.

There will probably be a better idea, but here’s a quick and dirty idea…

Flatten the 2D array to a 1D array by joining element 1 with element 2 as strings, split by (say) a ~ character

Apple~23
Apple~07
Zoom~76
Apple~01

Sort the one-dimensional array using array.sort
Apple~01
Apple~07
Apple~23
Zoom~76

Then fill your listbox using that, splitting as you go.
Or even better, use TAB as the separator, and after the sort has happened, you can just Addrow into the listbox, and the tab will make the 2 parts hit the right columns automatically…

you can make a sort method that return -1,0,1
so you put something in and out comes a value

in case of 2-dimensional array … emm - i must test myself
ok, the array.sort method is only for one dimension …

how about a class, you can store the instances into a array and you can sort this.
inside of this class is a property array with a list of the other dimension, you can sort this easy with the sort method

so your 2 dimemsion array turns into a list with each entry have a list

@Jeff Tullin: that sounds like a good idea.

@Markus Rauch: haven’t a clue what you mean.

If the data is going into a ListBox anyway, then use the ListBox to do the sorting. Create an event handler ListBox.CompareRows to compare the data. Then put the data in using AddRow, and call ListBox.Sort to sort it.

Again, it only sorts one column

But if a hidden column holds the other columns concatenated together, you could sort on that.
Probably need to hide the header…

Within the CompareRows event, you can consider whatever columns or data you’d like. This is likely the quickest way to do it if you don’t really need the original array sorted.

@Eric Bloom: I did that. But I have a reload button. This gets new data from the server without the sort information. Looks quite odd.

@Kem Tekinay : good idea. Doesn’t work because I need the original data sorted so that the reload button works.

Load the data into an in-memory sqlite database, do your sorting work using GROUP BY and ORDER BY and then add the result to the listbox.

@Natascha Chrobok : that’s going to be my last resort because it’s really overkill.

Can’t you call Sort after reloading?

Otherwise, go with Markus’ suggestion. Create a class to hold the row data and create a single dimension array of that class. You can then sort the array through a Delegate.

Hello,

Examble 3 column

https://www.dropbox.com/s/7paj1w884bl31vj/Arraysort-dreispaltig.xojo_binary_project?dl=1

@Rudolf Jackel : thanks, that looks like Jeff Tullins suggestions.

@Kem Tekinay : ah, that’s what Markus Rauch meant. I’ll try that.

Is it? Programmingwise maybe. But databases are very fast with sorting. You might lose some time with importing and exporting the data. But as it is in memory, that might not make much difference. I’d be interested which approach is faster in the end.

Denk Dir eine Liste mit Objekten wo bei jedes Objekt auch eine Liste haben kann.
Jede Liste läßt sich über eine Vergleichsfunktion sortieren.
Ob es Dir so nützt weiß ich nicht.

Liste Obj1 Liste A B C Obj2 Liste X Y Z

There is also the issue of rearranging rows in a 2D array since this will require copying to swap the data in rows
A db allows multi=key sorting really simply

Or build an index with links to the original data positions (or keys). The index could be sorted as necessary. Then access to data through getters. Only worth it for smaller 100s of nb, else a sqlite db would do the job.
Is it a very time sensitive function ?

Use Quicksort code written in Xojo that uses callbacks (or an Interface class), that asks you (a) to compare two values by their index, and (b) to swap two values by index. This separates the actually compared data from the the algorithm, which only needs to know which “item” is larger, without having to look at the data itself.

Assuming the first dimension are the rows, and the 2nd dimension are the columns, first sort each column individually, once.
When all cols are sorted, sort the rows.

I have some code for this, and I had published it very long ago. It should be in my old RB archive at http://files.tempel.org/RB/ or at http://www.tempel.org/RB/ but I can’t find it there right now…

Ah, found it. Case still matters on the Web :slight_smile: http://www.tempel.org/rb/ – Search for “sort”