Arrays - sortWith and delegate method?

Just wondering - does Xojo support sorting multiple arrays using a delegate comparison function ? ie along the lines of Array.SortWith(addressOf [delegateFunction], array1, array2… arrayN) ?

I know its possible to do what I want with a delegate function to sort a single array of indexes (aka a lookup table - without actually sorting the other arrays…) but just thought I’d ask - more out of curiosity; it would be really neat if it did, and clearly a ListBox does as much, since it can sort rows with a delegate function accessing whatever is in the rowTags or cellTags as the sort keys.

I have some very old code from Realbasic times where I can plug in a sort function. It’s used like this:

SortLibrary.Sort(AccountsLoaded, new NaturalSortComparator)

Not a delegate, but close.

No, but I do have a feature request for such a function.

https://tracker.xojo.com/xojoinc/xojo/-/issues/69805

The best I could do without that is to construct a string array (or other data type) that would sort how you wish the outcome to be and use the plain SortWith function.

For example, to sort by two different numbers you can construct a composite single number

Var SortByArray(9) as Integer
For i = 0 to 9
   SortByArray(i) = FirstSort(i) * 1000 + SecondSort(i) // Assuming values are 0 - 999
next
SortByArray.SortWith(FirstSort, SecondSort, AnyOtherData, etc)

As long as you can build an array that when sorted ‘in natural order’ would come out in the order you desire you can then sort that array along with the other data you wish to sort. Another example, sort a set of arrays by the first in descending order:

Var SortByArray(9) as Integer
For i = 0 to 9
   SortByArray(i) = 1000 - FirstSort(i) // Assuming values are 0 - 1000
next
SortByArray.SortWith(FirstSort, AnyOtherData, etc)

Hi Ian,

Not a problem - solved it in any case - my solution is an index sort - add an index property to the objects concerned, number the indexes sequentially, do the sort with a delegate function then extract the indexes from the sorted list to a separate array. Searching the index array is fast as they’re integers.

What’s got me even more intrigued is the speed of using array.indexOf to find anything, and how this scales with large arrays; it’s behaviour suggests it isn’t a linear search. I’m also looking at how the Xojo dictionary scales as well.

So far the array is consistently quicker to sort and to find an index than a dictionary (several thousand objects).

Kinda like my suggestion. Build an array that would have an ascending order from your data, then sort by that, together with your real data.