Difference between revisions of "SortWith"

From Xojo Documentation

m (1 revision)
 
 
(25 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 +
{{MethodBox
 +
| name=Sortwith
 +
| platform=all
 +
| scope=public
 +
| owner=[[Arrays]]
 +
| parameters=array1 [, ... arrayN]
 +
}}
 +
{{Description
 +
|text = Sorts one or more additional arrays in the same order as the base array. The sort is in ascending order. }}
  
 
+
== Usage ==
=='''Description'''==
+
''array''.'''Sortwith'''(''array1''[,...''arrayN''])
Sorts one or more additional arrays in the same order as the base array. The sort is in ascending order.
+
{| class="genericTable"
 
+
! width=15% | Part
 
+
! width=55% | Description
 
 
=='''Syntax'''==
 
array.'''Sortwith(array1[,...arrayN])'''
 
{| cellpadding="8" cellspacing="0" border="1"  
 
 
 
! width=25% style="background-color:#e0e0e0" | Part
 
 
 
! width=55% style="background-color:#e0e0e0" | Description
 
 
|-
 
|-
 
|array
 
|array
 
 
|The array to be sorted.
 
|The array to be sorted.
 
 
|-
 
|-
|''array1''
+
|array1
 
|The array to be sorted in the order determined by sorting the base array.
 
|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|OutOfBoundsException]] is thrown.
+
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''
+
|array2...arrayN
 
|Optional. Additional arrays to be sorted with the base array.  
 
|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.
 
These arrays must also have the same number of elements as the base array but can be of different data types.
 
 
|-
 
|-
 
|}
 
|}
  
 
+
== Notes ==
=='''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.
The base array used with the '''Sortwith''' method works with [[Integer|Integer]], [[String|string]], [[Single|single]], and [[Double|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.
 
'''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:
 +
<rbcode>
 +
Var names() As String = Array("Mozart", "Bing", "Jackson", "Flintstone")
 +
Var zips() As String = Array("04101", "04240", "04123", "04092")
  
 +
names.SortWith(zips)
  
=='''Examples'''==
+
// names() = "Bing", "Flintstone", "Jackson", "Mozart"
This example sorts the columns of the data table by the values in the aNames array.
+
// zips = "04240", "04092", "04123", "04101"
{| cellpadding="8" cellspacing="0" border="1"
+
</rbcode>
|<div style="width:100%; background-color:#d0d0d0; padding-left:30px">[[Dim|Dim]] aNames(), aAddresses(),aPhones() as [[String|String]]<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">aNames=[[Array|array]]("Mozart","Bing","Jackson","Flintstone")
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">aAddresses=[[Array|array]]("34 Pickwick","3424 Kenwood","432 Marlboro","1 Hardrock")
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">aPhones=[[Array|array]]("234-3700","697-5300","753-2500","None")
 
<br /></div>
 
<br />
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">aNames.'''Sortwith'''(aAddresses,aPhones)
 
<br /></div>
 
<br />
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">//display the sorted arrays in a Listbox to verify the sort
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">[[For|For]] i as [[Integer|Integer]]=0 to [[Ubound|ubound]](aNames)
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">&#xA0;ListBox1.addrow aNames(i)
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">&#xA0;ListBox1.cell(Listbox1.LastIndex,1) =aAddresses(i)
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">&#xA0;ListBox1.cell(ListBox1.LastIndex,2)= aPhones(i)
 
<br /></div>
 
<div style="width:100%; background-color:#d0d0d0; padding-left:30px">Next
 
<br /></div>
 
  
|-
+
Sort an array of objects using one of its values:
|}
+
<rbcode>
 +
// Person is a class containing a LastName property
 +
// 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)
  
=='''See Also'''==
+
// The People array is now sorted to match the last names
[[Dim|Dim]] statement; [[Array|Array]], [[Join|Join]], [[Split|Split]], [[Ubound|Ubound]] functions; [[Append|Append]], [[IndexOf|IndexOf]], [[Insert|Insert]], [[Pop|Pop]], [[Redim|Redim]], [[Remove|Remove]], [[Shuffle|Shuffle]], [[Sort|Sort]] methods; [[ParamArray|ParamArray]] keyword.
+
</rbcode>
  
 +
== See Also ==
 +
[[Var]] statement; [[Arrays]] concept for a complete list of functions; [[ParamArray]] keyword, [[Arrays.Sort]] method
  
[[Category:Arrays]]
+
[[Category:Language_Arrays]]

Latest revision as of 21:01, 6 December 2019

Method

Arrays.Sortwith(array1 [, ... arrayN])

Supported for all project types and targets.

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 names() As String = Array("Mozart", "Bing", "Jackson", "Flintstone")
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:

// Person is a class containing a LastName property
// 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