Created
April 2, 2023 21:49
-
-
Save Jeannot-Muller/f35fba09053f30f1ce0249b0e7532d78 to your computer and use it in GitHub Desktop.
multidimensionalArrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment