Sort (Arraylist subroutine): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
m (1 revision)
Line 1: Line 1:
{{Template:Arraylist:Sort subtitle}}
{{Template:Arraylist:Sort subtitle}}
[[Category:Arraylist methods|Sort subroutine]]
[[Category:Arraylist methods|Sort subroutine]]
<!--DPL?? Category:Arraylist methods|Sort subroutine: Sort the input Arraylist-->
<!--DPL?? Category:Arraylist methods|<var>Sort</var> subroutine: <var>Sort</var> the input Arraylist-->
<p>
<p>
Sort is a member of the [[Arraylist class]].
<var>Sort</var> is a member of the [[Arraylist class]].
</p>
</p>


Line 12: Line 12:
(a function that gets applied to each Arraylist item).
(a function that gets applied to each Arraylist item).
Each sort criterion pair is a [[SortOrder class|SortOrder]] object,
Each sort criterion pair is a [[SortOrder class|SortOrder]] object,
and multiple pairs are a SortOrder collection.
and multiple pairs are a <var>Sort</var>Order collection.


The sort key function that gets applied to each Arraylist item, which
The sort key function that gets applied to each Arraylist item, which
you identify in the Sort parameter, must
you identify in the <var>Sort</var> parameter, must
operate on the item type and return a User Language intrinsic
operate on the item type and return a User Language intrinsic
datatype (Float, String, Longstring, or Unicode) value.
datatype (Float, String, Longstring, or Unicode) value.
Line 23: Line 23:
The system intrinsic classes are discussed in "[[Intrinsic classes]]".
The system intrinsic classes are discussed in "[[Intrinsic classes]]".


Sort is available in ''Sirius Mods'' version 7.3 and later.
<var>Sort</var> is available in ''Sirius Mods'' version 7.3 and later.
==Syntax==
==Syntax==
{{Template:Arraylist:Sort syntax}}
{{Template:Arraylist:Sort syntax}}
Line 31: Line 31:
<td>The input Arraylist object. </td></tr>
<td>The input Arraylist object. </td></tr>
<tr><th><i>sortCriteria</i></th>
<tr><th><i>sortCriteria</i></th>
<td>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>
<td>One or more <var>Sort</var>Order 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 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 ''%arrayl'' 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]]".</td></tr>
  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 ''%arrayl'' 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]]".</td></tr>
</table>
</table>
==Usage notes==
==Usage notes==
<ul>
<ul>
<li>If the function applied by Sort returns string values, Sort
<li>If the function applied by <var>Sort</var> returns string values, <var>Sort</var>
uses the decimal-equivalent value of the character bytes to determine
uses the decimal-equivalent value of the character bytes to determine
the ascending or descending alphabetic order of the associated arraylist items.
the ascending or descending alphabetic order of the associated arraylist items.
Line 45: Line 45:
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
are applied, Sort places them in the same order in which they appear in the
are applied, <var>Sort</var> places them in the same order in which they appear in the
input Arraylist.
input Arraylist.
<li>The function in the parameter for Sort is a method value, not a User Language expression.
<li>The function in the parameter for <var>Sort</var> is a method value, not a User Language expression.
That is, you cannot provide a function that itself has an argument
That is, you cannot provide a function that itself has an argument
(say, <tt>ToIntegerPower(2)</tt>) as the Sort parameter.
(say, <tt>ToIntegerPower(2)</tt>) as the <var>Sort</var> parameter.
<li>As of ''Sirius Mods'' version 7.6, the default ''sortCriteria'' argument is
<li>As of ''Sirius Mods'' version 7.6, the default ''sortCriteria'' argument is
the SortOrder <tt>Ascending(This)</tt>,
the <var>Sort</var>Order <tt>Ascending(This)</tt>,
where <tt>This</tt> is the identity function described further
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]]".
in "[[Collections#Using the This function as the Maximum parameter|Using the This function as the Maximum parameter]]".
Line 57: Line 57:
specified simply as <tt>%arrayl:sort</tt>.
specified simply as <tt>%arrayl:sort</tt>.
See "[[SortNew (Arraylist function)#Examples|SortNew example]]".
See "[[SortNew (Arraylist function)#Examples|SortNew example]]".
<li>[[SortNew (Arraylist function)|SortNew]] is a function that works like Sort
<li>[[SortNew (Arraylist function)|SortNew]] is a function that works like <var>Sort</var>
but returns a new Arraylist instead of replacing the method object.
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]].
<li>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>
</ul>

Revision as of 23:58, 19 January 2011

Sort the input Arraylist (Arraylist class)

Sort is a member of the Arraylist class.

This subroutine sorts the method object Arraylist and replaces it with the sorted result. 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 Sort 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 Arraylist.

The system intrinsic classes are discussed in "Intrinsic classes".

Sort is available in Sirius Mods version 7.3 and later.

Syntax

al:Sort[( [sortOrder])]

Syntax terms

%arrayl The input Arraylist object.
sortCriteria 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 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:
     %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 items 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 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 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, Sort places them in the same order in which they appear in the input Arraylist.
  • 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 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:sort(ascending(this)) can be specified simply as %arrayl:sort. See "SortNew example".
  • SortNew is a function that works like Sort but returns a new Arraylist instead of replacing the method object.
  • For examples of the Sort method, see Finding collection maxima and minima, and sorting.