Sort (Arraylist subroutine): Difference between revisions
m (1 revision) |
mNo edit summary |
||
Line 1: | Line 1: | ||
{{Template:Arraylist:Sort subtitle}} | {{Template:Arraylist:Sort subtitle}} | ||
<var>Sort</var> sorts the method object <var>Arraylist</var> replacing it with the sorted result. The sort is based on one or more | <var>Sort</var> sorts the method object <var>Arraylist</var> replacing it with the sorted result. The sort is based on one or more sort criteria, each of which consists of a sorting direction (ascending or descending) paired with a sort key (a function that gets applied to an <var>Arraylist</var> item). The sort criteria pairs are passed to <var>SortNew</var> as a <var>[[SortOrder class|SortOrder]]</var> argument. | ||
<p> | |||
<p>Each sort key function (which is applied to the <var>Arraylist</var> items) must operate on the item type and return an <var>[[Intrinsic classes|intrinsic]]</var> datatype (<var>Float</var>, <var>String</var>, <var>Longstring</var>, or <var>Unicode</var>) value. The values returned by the function are sorted into ascending or descending order to determine the position of their associated item in the <var>Arraylist</var>.</p> | |||
==Syntax== | ==Syntax== |
Revision as of 22:16, 30 November 2011
Sort the input Arraylist (Arraylist class)
Sort sorts the method object Arraylist replacing it with the sorted result. The sort is based on one or more sort criteria, each of which consists of a sorting direction (ascending or descending) paired with a sort key (a function that gets applied to an Arraylist item). The sort criteria pairs are passed to SortNew as a SortOrder argument.
Each sort key function (which is applied to the Arraylist items) must operate on the item type and return an intrinsic datatype (Float, String, Longstring, or Unicode) value. The values returned by the function are sorted into ascending or descending order to determine the position of their associated item in the Arraylist.
Syntax
al:Sort[( [sortOrder])]
Syntax terms
al | The input Arraylist object. |
---|---|
sortOrder | One or more SortOrder objects, which consist of an ordering direction for the sort and an attribute, or key, to sort by. The ordering direction and sort key are specified in the form: order(key). The order is either Ascending or Descending , and the key is a function that is applied to each item in the al. The values returned by the function are sorted into ascending or descending order to determine the position of their associated item in the rearranged al. For example:
al:sort(descending(length)) The function is a method value (a method or class member name literal, or a method variable) for a method that operates on items of the type specified on the al declaration and that returns a numeric or string value. This is described further in "Specifying a SortOrder's sort key method". |
Usage notes
- If the function applied by Sort returns string values, Sort uses the decimal-equivalent value of the character bytes to determine the ascending or descending alphabetic order of the associated al items. Lowercase letters are first ranked alphabetically, then upper case letters, also ranked alphabetically, followed by the numbers; ie:
'a'..'z','A'..'Z',0..9
. - If two or more al items have equal values after all sort criteria are applied, Sort places them in the same order in which they appear in the input al.
- The function in the parameter for Sort is a method value, not a User Language expression. That is, you cannot provide a function that itself has an argument (say,
ToIntegerPower(2)
) as the Sort parameter. - As of Sirius Mods Version 7.6, the default sortOrder argument is the sortOrder
Ascending(This)
, where This is the identity function value described further in "Using the This function as the Maximum parameter". Therefore,al:sort(ascending(this))
can be specified simply asal:sort
. See the "SortNew example". - Sort is available in Sirius Mods Version 7.3 and later.
Examples
For examples of the Sort method, see "Finding collection maxima and minima, and sorting".
See also
- SortNew which works like Sort but returns a new Arraylist instead of replacing the method object.