SortNew (GenericNamedArraylist function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
Line 1: Line 1:
<span style="font-size:120%; color:black"><b><section begin=dpl_desc/>Sort NamedArraylist, return Arraylist<section end=dpl_desc/></b></span>
{{Template:GenericNamedArraylist:SortNew subtitle}}
[[Category:NamedArraylist methods|SortNew function]]
<!--DPL?? Category:NamedArraylist methods|SortNew function: Sort NamedArraylist, return Arraylist-->
<p>
SortNew is a member of the [[NamedArraylist class]].
</p>


This function sorts the method object NamedArraylist and returns the sorted result
This function sorts the method object NamedArraylist and returns the sorted result
Line 25: Line 20:
SortNew is available in ''Sirius Mods'' version 7.3 and later.
SortNew is available in ''Sirius Mods'' version 7.3 and later.
==Syntax==
==Syntax==
  %arlnew = %namrayl:SortNew(sortCriteria)
{{Template:GenericNamedArraylist:SortNew syntax}}
===Syntax Terms===
===Syntax terms===
<dl>
<dl>
<dt><i>%arlnew</i>
<dt><i>%arlnew</i>
Line 50: Line 45:


</dl>
</dl>
==Usage Notes==
==Usage notes==
<ul>
<ul>
<li>If the function applied by SortNew returns string values, SortNew
<li>If the function applied by SortNew returns string values, SortNew
Line 57: Line 52:
NamedArraylist items.
NamedArraylist items.
In ascending order,
In ascending order,
the highest or last lowercase letter is &ldquo;z&rdquo;;
the highest or last lowercase letter is "z";
the highest or last uppercase letter is &ldquo;Z&rdquo;;
the highest or last uppercase letter is "Z";
&ldquo;z&rdquo; ranks lower than all the uppercase letters;
"z" ranks lower than all the uppercase letters;
and all letters rank lower than any number.
and all letters rank lower than any number.
<li>If two or more NamedArraylist items have equal values after all sort criteria
<li>If two or more NamedArraylist items have equal values after all sort criteria
Line 151: Line 146:
     Michael Palin  (born: 19430505)
     Michael Palin  (born: 19430505)
</pre>
</pre>
==See also==
{{Template:GenericNamedArraylist:SortNew footer}}

Revision as of 20:53, 28 January 2011

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


This function sorts the method object NamedArraylist 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 NamedArraylist 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 NamedArraylist 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".

SortNew is available in Sirius Mods version 7.3 and later.

Syntax

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

Syntax terms

%arlnew
An Arraylist object to contain the sorted %namrayl items.
%namrayl
The input NamedArraylist object.
sortCriteria
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 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 %namrayl 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 NamedArraylist 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 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.
  • 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, %namrayl:sortnew(ascending(this) can be specified simply as %namrayl:sortnew. See the SortNew example.

Examples

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. For additional sorting examples, see Finding collection maxima and minima, and sorting.

    b
    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)

See also