SortNew (GenericNamedArraylist function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
mNo edit summary
 
(19 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Template:GenericNamedArraylist:SortNew subtitle}}
{{Template:GenericNamedArraylist:SortNew subtitle}}


This function sorts the method object NamedArraylist and returns the sorted result
<var>SortNew</var> sorts the method object <var>FloatNamedArraylist</var>, <var>NamedArraylist</var>, or <var>UnicodeNamedArraylist</var> and returns the sorted result in a new <var>[[Arraylist class|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 each <var>FloatNamedArraylist</var>, <var>NamedArraylist</var>, or <var>UnicodeNamedArraylist</var> item). The sort criteria pairs are passed to <var>SortNew</var> as a <var>[[SortOrder class|SortOrder]]</var> argument.
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 NamedArraylist item).
Each sort criterion pair is a [[SortOrder class|SortOrder]] object,
and multiple pairs are a SortOrder collection.


The sort key function that gets applied to each NamedArraylist item, which
In the <var>SortNew</var> parameter, you identify the sort key function that gets applied to each <var>FloatNamedArraylist</var>, <var>NamedArraylist</var>, or <var>UnicodeNamedArraylist</var> item.
you identify in the SortNew parameter, must
This function must
operate on the item type and return a User Language intrinsic
operate on that item type and return an <var>[[Intrinsic classes|intrinsic]]</var> datatype (<var>Float</var>, <var>String</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 result <var>Arraylist</var>.
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==
==Syntax==
{{Template:GenericNamedArraylist:SortNew syntax}}
{{Template:GenericNamedArraylist:SortNew syntax}}
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%arlnew</th>
<tr><th>%al</th>
<td>An Arraylist object to contain the sorted ''%namrayl'' items. </td></tr>
<td>An <var>Arraylist</var> object to contain the sorted <var class="term">anyNal</var> items. </td></tr>
<tr><th>%namrayl</th>
 
<td>The input NamedArraylist object. </td></tr>
<tr><th>anyNal</th>
<tr><th>sortCriteria</th>
<td>The input <var>FloatNamedArraylist</var>, <var>NamedArraylist</var>, or <var>UnicodeNamedArraylist</var> object. </td></tr>
<td>One or more SortOrder objects, which consist of an ordering direction for the sort and a key to sort by, 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. For example: <p class="code">%alist = %namray:sortnew(descending(length)) </p>
 
<p class="code">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 ''%namrayl'' 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>
<tr><th>sortOrder</th>
</p>
<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: <code>order(key)</code>. The order is either <code>Ascending</code> or <code>Descending</code>, and the key is a function that is applied to each item in the <var class="term">anyNal</var> <var>arrayList</var>. For example: <p class="code">%alist = %namray: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">anyNal</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 SortNew returns string values, SortNew
<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 decimal-equivalent value of the character bytes to determine
 
the ascending or descending alphabetic order of the associated
<li>If two or more <var class="term">anyNal</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">anyNal</var>.
NamedArraylist items.
 
In ascending order,
<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.
the highest or last lowercase letter is "z";
 
the highest or last uppercase letter is "Z";
<li>As of <var class="product">[[Sirius Mods|Sirius Mods]]</var> Version 7.6, the default <var>sortCriteria</var> 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">anyNal</var>:sortNew(ascending(this))</code> can be specified simply as <code><var class="term">anyNal</var>:sortNew</code>. See the [[SortNew (Arraylist function)#Examples|SortNew example]].
"z" ranks lower than all the uppercase letters;
 
and all letters rank lower than any number.
<li><var>SortNew</var> is available in <var class="product">Sirius Mods</var> Version 7.3 and later.
<li>If two or more NamedArraylist items have equal values after all sort criteria
are applied, SortNew returns them in the same order in which they appear in the
input NamedArraylist.
<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, <tt>Char(13)</tt>) as the SortNew 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>%namrayl:sortnew(ascending(this)</tt> can be
specified simply as <tt>%namrayl:sortnew</tt>.
See the [[SortNew (Arraylist function)#Examples|SortNew example]].
</ul>
</ul>
==Examples==
==Examples==
<ol><li>The following request displays a <var>NamedArraylist</var> in its normal sorted-by-label
order, then in order of a sort based on a <var>Variable</var> member of the user class.
<p class="code">begin
  class python
  public
      variable firstname is string len 16
      variable surname is string len 16
      variable birthdate is float
      constructor newpy (%sname is string len 16, -
              %name is string len 16, %bd is float)
      function myPrint is longstring
  end public


The following request displays a NamedArraylist in its normal sorted-by-label
  constructor newpy (%sname is string len 16, -
order, then in order of a sort based on a Variable member of the user class.
      %name is string len 16, %bd is float)
For additional sorting examples, see [[Collections#Finding collection maxima and minima, and sorting|Finding collection maxima and minima, and sorting]].
      %this:firstname = %name
<p class="code">b
      %this:surname = %sname
class python
      %this:birthdate = %bd
public
  end constructor
<p class="code">variable firstname is string len 16
variable surname is string len 16
variable birthdate is float
constructor newpy (%sname is string len 16, -
  %name is string len 16, %bd is float)
function myPrint is longstring
</p>
end public


constructor newpy (%sname is string len 16, -
  function myPrint is longstring
<p class="code">%name is string len 16, %bd is float)
      return %this:firstname with ' ' with -
%this:firstname = %name
          %this:surname with '  ' with -
%this:surname = %sname
          '(born: ' with %this:birthdate with ')'
%this:birthdate = %bd
  end function
</p>
end constructor
 
function myPrint is longstring
<p class="code">return %this:firstname with ' ' with -
      %this:surname with '  ' with -
      '(born: ' with %this:birthdate with ')'
</p>
end function


end class
  end class


%i is float
  %i is float
%lp is Arraylist of object python
  %lp is Arraylist of object python
%nlp is namedArraylist of object python
  %nlp is namedArraylist of object python


%nlp = new
  %nlp = new
%nlp('Gilliam') = newpy('Gilliam', 'Terry', '19401122')
  %nlp('Gilliam') = newpy('Gilliam', 'Terry', '19401122')
%nlp('Cleese') = newpy('Cleese', 'John', '19391027')
  %nlp('Cleese') = newpy('Cleese', 'John', '19391027')
%nlp('Idle') = newpy('Idle', 'Eric', '19430329')
  %nlp('Idle') = newpy('Idle', 'Eric', '19430329')
%nlp('Palin') = newpy('Palin', 'Michael', '19430505')
  %nlp('Palin') = newpy('Palin', 'Michael', '19430505')
%nlp('Chapman') = newpy('Chapman', 'Graham', '19410108')
  %nlp('Chapman') = newpy('Chapman', 'Graham', '19410108')
%nlp('Jones') = newpy('Jones', 'Terry', '19420201')
  %nlp('Jones') = newpy('Jones', 'Terry', '19420201')


for %i from 1 to %nlp:count
  for %i from 1 to %nlp:count
<p class="code">print %nlp:itembynumber(%i):myprint
      print %nlp:itembynumber(%i):myprint
</p>
  end for
end for


Print '======================================'
<nowiki>  print '======================================'</nowiki>
Print 'After the sort by birthdate:'
%lp = %nlp:sortnew(ascending(birthdate))


for %i from 1 to %lp:count
  print 'After the sort by birthdate:'
<p class="code">print %lp(%i):myprint
  %lp = %nlp:sortnew(ascending(birthdate))
</p>
end for


  for %i from 1 to %lp:count
      print %lp(%i):myprint
  end for
end
end
</p>
</p>
Line 127: Line 99:
Terry Jones  (born: 19420201)
Terry Jones  (born: 19420201)
Michael Palin  (born: 19430505)
Michael Palin  (born: 19430505)
======================================
<nowiki>======================================</nowiki>
After the sort by birthdate:
After the sort by birthdate:
John Cleese  (born: 19391027)
John Cleese  (born: 19391027)
Line 136: Line 108:
Michael Palin  (born: 19430505)
Michael Palin  (born: 19430505)
</p>
</p>
<li>This fragment is a simple <var>SortNew</var> example; the string items are sorted by their values:
<p class="code">%nal is namedArraylist of string len 30       
%nal = new                                     
%nal('Akron') = 'Rubber City'                 
%nal('Atlanta') = 'The Athens of the South'   
%nal('Philadelphia') = 'City of Brotherly love'
%al is arraylist of string len 30             
%al = %nal:sortNew(ascending)                 
%al:print                                     
</p>
The result is:
<p class="output">1: City of Brotherly love   
2: Rubber City               
3: The Athens of the South
</p>
<li>For additional sorting examples, see [[Collections#Finding collection maxima and minima, and sorting|"Finding collection maxima and minima, and sorting"]].</ol>
==See also==
==See also==
{{Template:GenericNamedArraylist:SortNew footer}}
{{Template:GenericNamedArraylist:SortNew footer}}

Latest revision as of 20:06, 1 November 2012

Sort items to an Arraylist (FloatNamedArraylist, NamedArraylist, and UnicodeNamedArraylist classes)


SortNew sorts the method object FloatNamedArraylist, NamedArraylist, or UnicodeNamedArraylist and returns 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 each FloatNamedArraylist, NamedArraylist, or UnicodeNamedArraylist item). The sort criteria pairs are passed to SortNew as a SortOrder argument.

In the SortNew parameter, you identify the sort key function that gets applied to each FloatNamedArraylist, NamedArraylist, or UnicodeNamedArraylist item. This function must operate on that item type and return an intrinsic datatype (Float, String, 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 result Arraylist.

Syntax

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

Syntax terms

%al An Arraylist object to contain the sorted anyNal items.
anyNal The input FloatNamedArraylist, NamedArraylist, or UnicodeNamedArraylist 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 anyNal arrayList. For example:

%alist = %namray: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 anyNal 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 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 anyNal items have equal values after all sort criteria are applied, SortNew returns them in the same order in which they appear in the input anyNal.
  • 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 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, anyNal:sortNew(ascending(this)) can be specified simply as anyNal:sortNew. See the SortNew example.
  • SortNew is available in Sirius Mods Version 7.3 and later.

Examples

  1. The following request displays a NamedArraylist in its normal sorted-by-label order, then in order of a sort based on a Variable member of the user class.

    begin class python public variable firstname is string len 16 variable surname is string len 16 variable birthdate is float constructor newpy (%sname is string len 16, - %name is string len 16, %bd is float) function myPrint is longstring end public constructor newpy (%sname is string len 16, - %name is string len 16, %bd is float) %this:firstname = %name %this:surname = %sname %this:birthdate = %bd end constructor function myPrint is longstring return %this:firstname with ' ' with - %this:surname with ' ' with - '(born: ' with %this:birthdate with ')' end function end class %i is float %lp is Arraylist of object python %nlp is namedArraylist of object python %nlp = new %nlp('Gilliam') = newpy('Gilliam', 'Terry', '19401122') %nlp('Cleese') = newpy('Cleese', 'John', '19391027') %nlp('Idle') = newpy('Idle', 'Eric', '19430329') %nlp('Palin') = newpy('Palin', 'Michael', '19430505') %nlp('Chapman') = newpy('Chapman', 'Graham', '19410108') %nlp('Jones') = newpy('Jones', 'Terry', '19420201') for %i from 1 to %nlp:count print %nlp:itembynumber(%i):myprint end for print '======================================' print 'After the sort by birthdate:' %lp = %nlp:sortnew(ascending(birthdate)) for %i from 1 to %lp:count print %lp(%i):myprint end for end

    The result is:

    Graham Chapman (born: 19410108) John Cleese (born: 19391027) Terry Gilliam (born: 19401122) Eric Idle (born: 19430329) Terry Jones (born: 19420201) Michael Palin (born: 19430505) ====================================== After the sort by birthdate: John Cleese (born: 19391027) Terry Gilliam (born: 19401122) Graham Chapman (born: 19410108) Terry Jones (born: 19420201) Eric Idle (born: 19430329) Michael Palin (born: 19430505)

  2. This fragment is a simple SortNew example; the string items are sorted by their values:

    %nal is namedArraylist of string len 30 %nal = new %nal('Akron') = 'Rubber City' %nal('Atlanta') = 'The Athens of the South' %nal('Philadelphia') = 'City of Brotherly love' %al is arraylist of string len 30 %al = %nal:sortNew(ascending) %al:print

    The result is:

    1: City of Brotherly love 2: Rubber City 3: The Athens of the South

  3. For additional sorting examples, see "Finding collection maxima and minima, and sorting".

See also