SortNew (Arraylist function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
m (match syntax diagram to template and fix tags)
Line 1: Line 1:
{{Template:Arraylist:SortNew subtitle}}
{{Template:Arraylist:SortNew subtitle}}


This function sorts the method object <var>Arraylist</var> and returns the sorted result
This function sorts the method object <var>Arraylist</var> and returns the sorted result in a new <var>Arraylist</var>. The sort is based on one or more sort [[SortOrder class|criteria]] which consist of a sorting direction (ascending or descending) paired with a sort key (a function that gets applied to each <var>Arraylist</var> item). Each sort criterion pair is a [[SortOrder class|SortOrder object]], and multiple pairs are a <var>SortOrder</var> collection.
in a new <var>Arraylist</var>.
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 <var>Arraylist</var> item).
Each sort criterion pair is a [[SortOrder class|SortOrder]] object,
and multiple pairs are a <var>SortOrder</var> collection.


The sort key function that gets applied to each <var>Arraylist</var> item, which
The sort key function that gets applied to each <var>Arraylist</var> item, which you identify in the <var>SortNew</var> parameter, must operate on the item type and return a User Language [[Intrinsic classes|intrinsic 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>.
you identify in the <var>SortNew</var> parameter, must
operate on the item type and return a User Language intrinsic
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>.


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


<var>SortNew</var> is available in ''Sirius Mods'' version 7.3 and later.
==Syntax==
==Syntax==
{{Template:Arraylist:SortNew syntax}}
{{Template:Arraylist:SortNew syntax}}
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th><i>%arlnew</i></th>
<tr><th><i>%newList</i></th>
<td>An <var>Arraylist</var> object to contain the sorted <var class="term">al</var> items.</td></tr>
<td>An <var>Arraylist</var> object to contain the sorted <var class="term">al</var> items.</td></tr>
<tr><th><i><var class="term">al</var></i></th>
<tr><th><i><var class="term">al</var></i></th>
<td>The input <var>Arraylist</var> object.</td></tr>
<td>The input <var>Arraylist</var> object.</td></tr>
<tr><th><i>sortCriteria</i></th>
<tr><th><i>sortOrder</i></th>
<td>One or more <var>SortOrder</var> objects, which consist of an ordering direction for the sort and a key to sort by, specified in the form: <var class="term">order(key)</var>. The <var class="term">order</var> is either <var>Ascending</var> or <var>Descending</var>, and the <var class="term">key</var> is a function that is applied to each item in the <var>Arraylist</var>. For example:
<td>One or more <var>SortOrder</var> objects, which consist of an ordering direction for the sort and a key to sort by, specified in the form: <var class="term">order(key)</var>. The <var class="term">order</var> is either <code>Ascending</code> or <var>Descending</code>, and the <var class="term">key</var> is a function that is applied to each item in the <var>al</var>. For example:
<p class="code">%alist = %alist:sortnew(descending(length)
<p class="code">%alist = %alist:sortnew(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 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]]".</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 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]]".</td></tr>
</table>
</table>


==Usage notes==
==Usage notes==
<ul>
<ul>
<li>If the function applied by <var>SortNew</var> returns string values, <var>SortNew</var>
<li>If the function applied by <var>sortNew</var> returns string values, <var>sortNew</var> uses the decimal-equivalent value of the character bytes to determine the ascending or descending alphabetic order of the associated <var class="term">al</var> items. In ascending order, the highest or last lowercase letter is <code>'z'</code>; the highest or last uppercase letter is <code>'Z'</code>; <code>'z'</code> ranks lower than all the uppercase letters; and all letters rank lower than any number.
uses the decimal-equivalent value of the character bytes to determine
<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>.
the ascending or descending alphabetic order of the associated arraylist items.
<li>The function in the parameter for <var>SortNew</var> is a method value, not a User Language expression. That is, you cannot provide a function that itself has an argument (say, <code>Char(13)</code>) as the <var>SortNew</var> parameter.
In ascending order,
<li>As of <var class="product">Sirius Mods</var> version 7.6, the default ''sortCriteria'' argument is the <var>SortOrder</var> <code>Ascending(This)</code>, where <code>This</code> 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, <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.
the highest or last lowercase letter is "z";
<li><var>SortNew</var> is available in <var class="product">Sirius Mods</var> version 7.3 and later.
the highest or last uppercase letter is "Z";
"z" ranks lower than all the uppercase letters;
and all letters rank lower than any number.
<li>If two or more <var>Arraylist</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>Arraylist</var>.
<li>The function in the parameter for <var>SortNew</var> is a method value, not a User Language expression.
That is, you cannot provide a function that itself has an argument
(say, <tt>Char(13)</tt>) as the <var>SortNew</var> parameter.
<li>As of ''Sirius Mods'' version 7.6, the default ''sortCriteria'' argument is
the <var>SortOrder</var> <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><var class="term">al</var>:sortnew(ascending(this))</tt> can be
specified simply as <tt><var class="term">al</var>:sortnew</tt>, as shown in the [[#Examples|example]] below.
<li>[[Sort (Arraylist subroutine)|Sort]] is a subroutine that works like the <var>SortNew</var> function
but applies the sort to the method object instead of returning a new <var>Arraylist</var>.
</ul>
</ul>
==Examples==
==Examples==
<ol><li>
The following simple example shows the default sort of an arraylist 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:
<p class="code">begin
  %al    is arraylist of float
  %al2  is arraylist of float
  %i    is float


The following simple example shows the default sort of an arraylist of integers.
   %al = list(9, 11, 4, -5, 17, 3, 6)
Sortnew is specified with no explicit argument, and <tt>ascending(this)</tt>
  %al2 = %al:sortnew
is the <var>SortOrder</var> argument that is invoked:
  for %i from 1 to %al2:count
<p class="code">b
      print %al2(%i)
%al   is arraylist of float
  end for
%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
<p class="code">print %al2(%i)
</p>
end for
 
end
end
</p>
</p>
Line 88: Line 56:
17
17
</p>
</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>


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]].
==See also==
==See also==
<ul><li><var>[[Sort (Arraylist subroutine)|Sort]]</var> is a subroutine that 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}}
{{Template:Arraylist:SortNew footer}}

Revision as of 22:50, 30 January 2011

Return new sorted Arraylist (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".

Syntax

%newList = al:SortNew[( [sortOrder])]

Syntax terms

%newList An Arraylist object to contain the sorted al items.
al The input Arraylist object.
sortOrder 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 al. 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".

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 al 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 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 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, al:sortnew(ascending(this)) can be specified simply as al:sortnew, as shown in the example below.
  • SortNew is available in Sirius Mods version 7.3 and later.

Examples

  1. 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

  2. For more examples of the SortNew method, see Finding collection maxima and minima, and sorting.

See also

  • Sort is a subroutine that works like the sortNew function but applies the sort to the method object instead of returning a new al.