How-To Sort Multidimensional Array In Xojo

How-To Sort Multidimensional Array In Xojo

In Xojo it is easy to sort one-dimensional arrays. You just write yourArray.Sort and you are done.

Things are getting a little bit more complicated if you have to sort so-called multidimensional arrays, which means an array with multiple values. Out-of-the-box Xojo can’t sort these arrays.

But with a simple re-design of your application, it is relatively easy to get this achieved. You can store your values in your class, and the instances of that class in a one-dimensional array.

Now Xojo can’t sort this one-dimensional array either, because it contains classes and not strings for instance, but you can write your method to sort your array, as I will show below.

The main code is straightforward:

Please note that we are writing our own method to sort our one-dimensional array, containing the class instances. This is done via the command:

results.sort(addressof vehicleCompareCar )

VehicleCompareCar is the methodname to do this sorting, which again is straightforward:

var result as integer result = value1.car.Compare( value2.car, ComparisonOptions.CaseSensitive ) return result

I decided to sort by car type in this example, but of course, I could easily sort by whatever other field I’m interested in.

In conclusion: if you are in the need of sorting a multidimensional array with Xojo, it is best to convert it first into class instances which you put into a one-dimensional array.

Did you find this article valuable?

Support Jeannot Muller by becoming a sponsor. Any amount is appreciated!