SortNew (Arraylist function): Difference between revisions
m (1 revision) |
m (1 revision) |
||
Line 1: | Line 1: | ||
{{Template:Arraylist:SortNew subtitle}} | |||
[[Category:Arraylist methods|SortNew function]] | [[Category:Arraylist methods|SortNew function]] | ||
<!--DPL?? Category:Arraylist methods|SortNew function: Return new sorted Arraylist--> | <!--DPL?? Category:Arraylist methods|SortNew function: Return new sorted Arraylist--> | ||
Line 24: | Line 24: | ||
SortNew is available in ''Sirius Mods'' version 7.3 and later. | SortNew is available in ''Sirius Mods'' version 7.3 and later. | ||
==Syntax== | |||
{{Template:Arraylist:SortNew syntax}} | |||
===Syntax terms=== | |||
<dl> | <dl> | ||
<dt><i>%arlnew</i> | <dt><i>%arlnew</i> | ||
Line 50: | Line 50: | ||
</dl> | </dl> | ||
==Usage notes== | |||
<ul> | <ul> | ||
<li>If the function applied by SortNew returns string values, SortNew | <li>If the function applied by SortNew returns string values, SortNew | ||
Line 56: | Line 56: | ||
the ascending or descending alphabetic order of the associated arraylist items. | the ascending or descending alphabetic order of the associated arraylist items. | ||
In ascending order, | In ascending order, | ||
the highest or last lowercase letter is | the highest or last lowercase letter is "z"; | ||
the highest or last uppercase letter is | the highest or last uppercase letter is "Z"; | ||
"z" ranks lower than all the uppercase letters; | |||
and all letters rank lower than any number. | and all letters rank lower than any number. | ||
<li>If two or more Arraylist items have equal values after all sort criteria | <li>If two or more Arraylist items have equal values after all sort criteria | ||
Line 75: | Line 75: | ||
but applies the sort to the method object instead of returning a new Arraylist. | but applies the sort to the method object instead of returning a new Arraylist. | ||
</ul> | </ul> | ||
==Examples== | |||
The following simple example shows the default sort of an arraylist of integers. | The following simple example shows the default sort of an arraylist of integers. |
Revision as of 23:27, 19 January 2011
Return new sorted Arraylist (Arraylist class)
SortNew is a member of the Arraylist class.
This function sorts the method object Arraylist and returns the sorted result in a new Arraylist. The sort is based on one or more sort criteria which consist of a sorting direction (ascending or descending) paired with a sort key (a function that gets applied to each Arraylist item). Each sort criterion pair is a SortOrder object, and multiple pairs are a SortOrder collection.
The sort key function that gets applied to each Arraylist item, which you identify in the SortNew parameter, must operate on the item type and return a User Language 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 new Arraylist.
The system intrinsic classes are discussed in "Intrinsic classes".
SortNew is available in Sirius Mods version 7.3 and later.
Syntax
%newList = al:SortNew[( [sortOrder])]
Syntax terms
- %arlnew
- An Arraylist object to contain the sorted %arrayl items.
- %arrayl
- The input Arraylist object.
- sortCriteria
- One or more SortOrder objects, which consist of an ordering direction for
the sort and a key to sort by, 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 Arraylist.
For example:
%alist = %arrayl:sortnew(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 objects of the type specified on the %arrayl 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 SortNew returns string values, SortNew uses the decimal-equivalent value of the character bytes to determine the ascending or descending alphabetic order of the associated arraylist items. In ascending order, the highest or last lowercase letter is "z"; the highest or last uppercase letter is "Z"; "z" ranks lower than all the uppercase letters; and all letters rank lower than any number.
- If two or more Arraylist items have equal values after all sort criteria are applied, SortNew returns them in the same order in which they appear in the input Arraylist.
- The function in the parameter for SortNew is a method value, not a User Language expression. That is, you cannot provide a function that itself has an argument (say, Char(13)) as the SortNew parameter.
- As of Sirius Mods version 7.6, the default sortCriteria argument is the SortOrder Ascending(This), where This is the identity function described further in "Using the This function as the Maximum parameter". Therefore, %arrayl:sortnew(ascending(this)) can be specified simply as %arrayl:sortnew, as shown in the example below.
- Sort is a subroutine that works like the SortNew function but applies the sort to the method object instead of returning a new Arraylist.
Examples
The following simple example shows the default sort of an arraylist of integers. Sortnew is specified with no explicit argument, and ascending(this) is the SortOrder argument that is invoked:
b %al is arraylist of float %al2 is arraylist of float %i is float %al = list(9, 11, 4, -5, 17, 3, 6) %al2 = %al:sortnew for %i from 1 to %al2:count print %al2(%i) end for end
The result is:
-5 3 4 6 9 11 17
For more examples of the SortNew method, see Finding collection maxima and minima, and sorting.