From Xojo Documentation

Revision as of 18:53, 19 November 2009 by Npalardy (talk | contribs) (1 revision)
You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!


Sorts one or more additional arrays in the same order as the base array. The sort is in ascending order.



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.


The base array used with the Sortwith method works with Integer, string, single, and double arrays only. It accepts only one-dimensional arrays and it should have non-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.


This example sorts the columns of the data table by the values in the aNames array.

Dim aNames(), aAddresses(),aPhones() as String
aAddresses=array("34 Pickwick","3424 Kenwood","432 Marlboro","1 Hardrock")


//display the sorted arrays in a Listbox to verify the sort
For i as Integer=0 to ubound(aNames)
 ListBox1.addrow aNames(i)
 ListBox1.cell(Listbox1.LastIndex,1) =aAddresses(i)
 ListBox1.cell(ListBox1.LastIndex,2)= aPhones(i)

See Also

Dim statement; Array, Join, Split, Ubound functions; Append, IndexOf, InsertPop, Redim, Remove, Shuffle, Sort methods; ParamArray keyword.