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, 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 an own class, and the instances of that class in an 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 an own method to sort your array, as I will show below.
You can download the above dummy project here:
The main code is straight forward:
// Defining a single dimensioned array, which will contain all our cars var results() as vehicle // Creating 6 cars (with drivers) // the id is only for information purposes for x as integer = 0 to 5 var result as new vehicle result.id = x select case x case 0 result.driver = "Marcel" result.car = "Mercedes" case 1 result.driver = "Jeannot" result.car = "Hyundai" case 2 result.driver = "Anton" result.car = "Alfa Romeo" case 3 result.driver = "Silke" result.car = "Toyota" case 4 result.driver = "Xavier" result.car = "Citroen" case 5 result.driver = "Pluto" result.car = "Porsche" end select results.AddRow(result) next x var output as string // Show the created cars by exporting them through // looping over our array output = "RESULTS w/o SORTING:" + endofline + endofline For each result as vehicle in results output = output + result.driver + " | " + result.car + EndOfLine next output = output + endofline // You can't sort "classes" in an array and you can't reference to // properties of the classes in this array, but you can create your own // method for sorting and reference to this method // Please note that auto completion will not work in the IDE for this sort kind // of sort statement. Even the word "sort" will not be shown as the Xojo IDE knows that // an array can not be sorted (out-of-the-box) if it contains classes. results.sort(addressof vehicleCompareCar ) // Exporting the array again, is now showing that the entries got sorted. // Mission accomplished. So in case you have the need of a multidimensional array, it is best // to transfer the variables into a class and add those classes to a one-dimensional array, // which you can sort with your own logic. output = output + "RESULTS after SORTING:" + endofline + endofline For each result as vehicle in results output = output + result.driver + " | " + result.car + EndOfLine next taResults.value = output
Please note that we are writing an 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.
Sign in or become a blog.xojoDOCs.com member to join the conversation.
Just enter your email below to get a log in link.