Sort (Arraylist subroutine): Difference between revisions

From m204wiki
Jump to navigation Jump to search
(Created page with "<span style="font-size:120%; color:black"><b><section begin=dpl_desc/>Sort the input Arraylist<section end=dpl_desc/></b></span> Sort subroutine <!...")
 
 
(41 intermediate revisions by 7 users not shown)
Line 1: Line 1:
<span style="font-size:120%; color:black"><b><section begin=dpl_desc/>Sort the input Arraylist<section end=dpl_desc/></b></span>
{{Template:Arraylist:Sort subtitle}}
[[Category:Arraylist methods|Sort subroutine]]
<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.
<!--DPL?? Category:Arraylist methods|Sort subroutine: Sort the input Arraylist-->
<p>
Sort is a member of the [[Arraylist class]].
</p>


This subroutine sorts the method object Arraylist and replaces it with the
<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>
sorted result.
The sort is based on one or more sort criteria which consist of
==Syntax==
a sorting direction (ascending or descending) paired with a sort key
{{Template:Arraylist:Sort syntax}}
(a function that gets applied to each Arraylist item).
===Syntax terms===
Each sort criterion pair is a [[SortOrder class|SortOrder]] object,
<table class="syntaxTable">
and multiple pairs are a SortOrder collection.
<tr><th>al</th>
<td>The input <var>Arraylist</var> object. </td></tr>
<tr><th>sortOrder</th>
<td>One or more <var>[[SortOrder class#SortOrder object variable declaration syntax|SortOrder]]</var> 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: <var class="term">order(key)</var>. The order is either <code>Ascending</code> or <code>Descending</code>, and the <var class="term">key</var> is a function that is applied to each item in the <var class="term">al</var>. The values returned by the function are sorted into ascending or descending order to determine the position of their associated item in the rearranged <var class="term">al</var>. For example:
<p class="code"><var class="term">al</var>:sort(descending(length))</p>
<p>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 <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"]].</p></td></tr>
</table>


The sort key function that gets applied to each Arraylist item, which
==Usage notes==
you identify in the Sort parameter, must
<ul>
operate on the item type and return a User Language intrinsic
<li>If the function applied by <var>Sort</var> returns <var>String</var> or <var>Unicode</var> values, <var>Sort</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.
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.


The system intrinsic classes are discussed in "[[Intrinsic classes]]".
<li>If two or more <var class="term">al</var> items have equal values after all sort criteria are applied, <var>Sort</var> places them in the same order in which they appear in the input <var class="term">al</var>.


Sort is available in ''Sirius Mods'' version 7.3 and later.
<li>The function in the parameter for <var>Sort</var> is a method value, not a <var class="product">User Language</var> expression.  That is, you cannot provide a function that itself has an argument (say, <code><var>[[ToIntegerPower and ToPower (Float functions)|ToIntegerPower]]</var>(2)</code>) as the <var>Sort</var> parameter.
===Syntax===
  [Call] %arrayl:Sort(sortCriteria)
====Syntax Terms====
<dl>
<dt><i>%arrayl</i>
<dd>The input Arraylist object.
<dt><i>sortCriteria</i>
<dd>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: <tt>order(key)</tt>.
The order is either <tt>Ascending</tt> or <tt>Descending</tt>, and
the key is a function that is applied to each item in the Arraylist.
The values returned by the function are sorted into ascending or descending order
to determine the position of their associated item in the rearranged Arraylist.
For example:
<pre style="xmp">
    %arrayl:sortnew(descending(length))
</pre>


The function is
<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>:sort(ascending(this))</code> can be specified simply as <code><var class="term">al</var>:sort</code>.  See the [[SortNew (Arraylist function)#Examples|"SortNew example"]].
a method value (a method or class member name literal, or a method variable)
for a method
<li><var>Sort</var> is available in <var class="product">[[Sirius Mods|Sirius Mods]]</var> Version 7.3 and later.
that operates on items of the type specified on the ''%arrayl''
</ul>
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]]".


</dl>
==Examples==
===Usage Notes===
For examples of the <var>Sort</var> method, see [[Collections#Finding collection maxima and minima, and sorting|"Finding collection maxima and minima, and sorting"]].
<ul>
<li>If the function applied by Sort returns string values, Sort
==See also==
uses the decimal-equivalent value of the character bytes to determine
<ul><li><var>[[SortNew (Arraylist function)|SortNew]]</var> which works like <var>Sort</var> but returns a new <var>Arraylist</var> instead of replacing the method object.</ul>
the ascending or descending alphabetic order of the associated arraylist items.
{{Template:Arraylist:Sort footer}}
In ascending order,
the highest or last lowercase letter is &ldquo;z&rdquo;;
the highest or last uppercase letter is &ldquo;Z&rdquo;;
&ldquo;z&rdquo; ranks lower than all the uppercase letters;
and all letters rank lower than any number.
<li>If two or more Arraylist items have equal values after all sort criteria
are applied, Sort places them in the same order in which they appear in the
input Arraylist.
<li>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, <tt>ToIntegerPower(2)</tt>) as the Sort parameter.
<li>As of ''Sirius Mods'' version 7.6, the default ''sortCriteria'' argument is
the SortOrder <tt>Ascending(This)</tt>,
where <tt>This</tt> is the identity function described further
in "[[Collections#Using the This function as the Maximum parameter|Using the This function as the Maximum parameter]]".
Therefore, <tt>%arrayl:sort(ascending(this))</tt> can be
specified simply as <tt>%arrayl:sort</tt>.
See "[[SortNew (Arraylist function)#Examples|SortNew example]]".
<li>[[SortNew (Arraylist function)|SortNew]] is a function that works like Sort
but returns a new Arraylist instead of replacing the method object.
<li>For examples of the Sort method, see [[Collections#Finding collection maxima and minima, and sorting|Finding collection maxima and minima, and sorting]].
</ul>

Latest revision as of 19:37, 13 August 2012

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 or Unicode values, Sort 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, 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 as al: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.