SortNew (Arraylist function): Difference between revisions
m (1 revision) |
m (→Usage notes) |
||
(46 intermediate revisions by 7 users not shown) | |||
Line 1: | Line 1: | ||
{{Template:Arraylist:SortNew subtitle}} | {{Template:Arraylist:SortNew subtitle}} | ||
The | <var>SortNew</var> sorts the method object <var>Arraylist</var> returning the sorted result in a new <var>Arraylist</var>. 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>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 new <var>Arraylist</var>.</p> | |||
==Syntax== | ==Syntax== | ||
{{Template:Arraylist:SortNew syntax}} | {{Template:Arraylist:SortNew syntax}} | ||
===Syntax terms=== | ===Syntax terms=== | ||
< | <table class="syntaxTable"> | ||
< | <tr><th>%newList</th> | ||
< | <td>A newly create <var>Arraylist</var> object to contain the sorted <var class="term">al</var> items.</td></tr> | ||
< | <tr><th>al</th> | ||
< | <td>The input <var>Arraylist</var> object.</td></tr> | ||
< | <tr><th>sortOrder</th> | ||
< | <td>A <var>SortOrder</var> object, which consists of one or more pairs of an ordering direction for the sort and a sort key function. For example: | ||
the sort and a | <p class="code">%alist = %alist:sortnew(descending(length)) | ||
</p> | |||
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 <var class="term">al</var> declaration and that returns a numeric or string value. This is described further in [[SortOrder class#Specifying a SortOrder's sort key method|"Specifying a SortOrder's sort key method"]]. | |||
For example: | |||
< | |||
</ | |||
A <var>SortOrder</var> with multiple sort criteria can be specified using the <var>[[List (SortOrder function)|List]]</var> function. | |||
</td></tr> | |||
</table> | |||
==Usage notes== | ==Usage notes== | ||
<ul> | <ul> | ||
<li>If the function applied by | <li>If the function applied by <var>sortNew</var> returns <var>String</var> or <var>Unicode</var> values, <var>SortNew</var> uses the collating sequence of EBCDIC or Unicode, respectively, to determine the ascending or descending alphabetic order of the associated <var class="term">al</var> items. If the function returns a numeric type, numeric comparisons are used. | ||
uses the | |||
the ascending or descending alphabetic order of the associated | <li>If two or more <var class="term">al</var> items have equal values after all sort criteria are applied, <var>sortNew</var> returns them in the same order in which they appear in the input <var class="term">al</var>. | ||
<li>The function in the parameter for <var>SortNew</var> is a method value, not a <var class="product">User Language</var> expression. That is, you cannot provide a function that, itself, also has an argument (say, <code>Char(13)</code>) as the <var>SortNew</var> parameter. | |||
<li>As of <var class="product">[[Sirius Mods|Sirius Mods]]</var> Version 7.6, the default <var class="term">sortOrder</var> argument is the <var>SortOrder</var> <code>Ascending(This)</code>, where <var>This</var> is the identity function value described further in [[Collections#Using the This function as the Maximum parameter|"Using the This function as the Maximum parameter"]]. Therefore, <code><var class="term">al</var>:sortnew(ascending(this))</code> can be specified simply as <code><var class="term">al</var>:sortnew</code>, as shown in the [[#Examples|example]] below. | |||
<li>If two or more | <li><var>SortNew</var> is available in <var class="product">Sirius Mods</var> Version 7.3 and later. | ||
are applied, | |||
input | |||
<li>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, < | |||
<li>As of | |||
the SortOrder < | |||
where < | |||
in | |||
Therefore, < | |||
specified simply as < | |||
<li> | |||
</ul> | </ul> | ||
==Examples== | ==Examples== | ||
<ol><li> | |||
The following simple example shows the default sort of an arraylist of integers. | The following simple example shows the default sort of an <var>arraylist</var> of integers. <var>Sortnew</var> is specified with no explicit argument, and <code>ascending(this)</code> is the <var>SortOrder</var> argument that is invoked: | ||
Sortnew is specified with no explicit argument, and < | <p class="code">begin | ||
is the SortOrder argument that is invoked: | %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) | print %al2(%i) | ||
end for | |||
end | |||
</p> | |||
</ | |||
The result is: | The result is: | ||
< | <p class="output">-5 | ||
3 | |||
4 | |||
6 | |||
9 | |||
11 | |||
17 | |||
</p> | |||
</ | <li>For more examples of the <var>SortNew</var> method, see [[Collections#Finding collection maxima and minima, and sorting|"Finding collection maxima and minima, and sorting"]].</ol> | ||
==See also== | |||
<ul><li><var>[[Sort (Arraylist subroutine)|Sort]]</var> which works like the <var>sortNew</var> function but applies the sort to the method object instead of returning a new <var class="term">al</var>.</ul> | |||
{{Template:Arraylist:SortNew footer}} |
Latest revision as of 19:36, 13 August 2012
Return new sorted Arraylist (Arraylist class)
SortNew sorts the method object Arraylist returning the sorted result in a new Arraylist. 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 new Arraylist.
Syntax
%newList = al:SortNew[( [sortOrder])]
Syntax terms
%newList | A newly create Arraylist object to contain the sorted al items. |
---|---|
al | The input Arraylist object. |
sortOrder | A SortOrder object, which consists of one or more pairs of an ordering direction for the sort and a sort key function. For example:
%alist = %alist: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 al declaration and that returns a numeric or string value. This is described further in "Specifying a SortOrder's sort key method". A SortOrder with multiple sort criteria can be specified using the List function. |
Usage notes
- If the function applied by sortNew returns String or Unicode values, SortNew uses the collating sequence of EBCDIC or Unicode, respectively, to determine the ascending or descending alphabetic order of the associated al items. If the function returns a numeric type, numeric comparisons are used.
- If two or more al items have equal values after all sort criteria are applied, sortNew returns them in the same order in which they appear in the input al.
- 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, also has an argument (say,
Char(13)
) as the SortNew 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:sortnew(ascending(this))
can be specified simply asal:sortnew
, as shown in the example below. - SortNew is available in Sirius Mods Version 7.3 and later.
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:begin %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".
See also
- Sort which works like the sortNew function but applies the sort to the method object instead of returning a new al.