SortWith
From Xojo Documentation
Sorts one or more additional arrays in the same order as the base array. The sort is in ascending order.
Usage
array.Sortwith(array1[,...arrayN])
Part | Description |
---|---|
array | The array to be sorted. |
array1 | The array to be sorted in the order determined by sorting the base array.
Array1 must have the same number of elements as the base array but can be of another data type. If the number of elements does not match, an OutOfBoundsException is thrown. |
array2...arrayN | Optional. Additional arrays to be sorted with the base array.
These arrays must also have the same number of elements as the base array but can be of different data types. |
Notes
The base array used with the Sortwith method works with Integer (and related types), String, Text, Single, and Double arrays only. It accepts only one-dimensional arrays and it should have unique values. Results are undefined when the elements of the base array are not unique, i.e., elements of the base array are all of the same integer value.
Sortwith is ideal for sorting all the columns of a data table by one of its columns. For example, if you have a set of three arrays that store Names, Addresses, and Phone numbers of a group of people, you can sort the data table by Name by specifying the Names array as the base array and pass the Address and Phone number arrays as the parameters.
Sample Code
Sort two related arrays:
Var zips() As String = Array("04101", "04240", "04123", "04092")
names.SortWith(zips)
// names() = "Bing", "Flintstone", "Jackson", "Mozart"
// zips = "04240", "04092", "04123", "04101"
Sort an array of objects using one of its values:
// Assume there is an array called People() As Person that contains
// a collection of Person objects
// Save the last names into their own array
Var lastNames() As String
For i As Integer = 0 To People.LastRowIndex
lastNames.Add(People(i).LastName)
Next
// Now sort the last names and provide the people array
lastNames.SortWith(People)
// The People array is now sorted to match the last names
See Also
Var statement; Arrays concept for a complete list of functions; ParamArray keyword, Arrays.Sort method