Ascending and Descending (SortOrder functions): Difference between revisions
No edit summary |
|||
(16 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{{Template:SortOrder:Ascending and Descending subtitle}} | {{Template:SortOrder:Ascending and Descending subtitle}} | ||
These shared methods create a new instance of the <var>SortOrder</var> class. | These [[Notation conventions for methods#Shared members|shared methods]] create a new instance of the <var>SortOrder</var> class. The ordering direction of the new instance is given by the method name: | ||
The ordering direction of the new instance is given by the | |||
<var>Ascending</var> is low-to-high; <var>Descending</var> is high-to-low. | <var>Ascending</var> is low-to-high; <var>Descending</var> is high-to-low. | ||
The sorting key of the new instance is specified by the input parameter | The sorting key of the new instance is specified by the input parameter | ||
to the | to the method. | ||
This parameter is a function that gets | This parameter is a function that gets | ||
applied to each item in the collection that is being sorted, and it must be | applied to each item in the collection that is being sorted, and it must be | ||
Line 20: | Line 19: | ||
<tr><th>%sortOrder</th> | <tr><th>%sortOrder</th> | ||
<td>A <var>SortOrder</var> object variable to contain the new object instance. </td></tr> | <td>A <var>SortOrder</var> object variable to contain the new object instance. </td></tr> | ||
<tr><th><var>%(SortOrder For</var> | <tr><th nowrap="true"><var>[%(SortOrder For </var>itemType<var>)]</var> | ||
<td>This optional specification of the class in parentheses | <td>This optional specification of the class in parentheses denotes a [[Notation conventions for methods#Constructors|virtual constructor]]. See [[#Usage notes|"Usage notes"]], below, for more information about invoking a <var>SortOrder</var> virtual constructor. </td></tr> | ||
<tr><th>itemFunction</th> | <tr><th>itemFunction</th> | ||
<td>A method value (a method name literal, a method variable, a class <var>Variable</var>, | <td>A method value (a method name literal, a method variable, a class <var>Variable</var>, or even a method that returns a method value) for a method that operates on objects of the type specified on the declaration of the collection being sorted, and that returns a numeric or string value. | ||
or even a method that returns a method value) for a method | <p> | ||
that operates on objects of the type specified on the | The default <var class="term">itemFunction</var> value as of <var class="product">Sirius Mods</var> version 7.6 is the special value <var>This</var>, described further in the "Usage Notes," below. </p></td></tr> | ||
declaration of the collection being sorted, and that returns a numeric or | |||
string value. | |||
< | |||
The default <var class="term">itemFunction</var> value as of <var class="product">Sirius Mods</var> version 7.6 is the special value <var>This</var>, described further in the "Usage Notes," below. </td></tr> | |||
</table> | </table> | ||
==Usage Notes== | ==Usage Notes== | ||
<ul> | <ul> | ||
<li>As described in [[Object variables#Virtual Constructor methods|"Virtual Constructor methods"]], <var>Ascending</var> and <var>Descending</var> can be invoked with | |||
no method object, with an explicit class specification, or with an object variable of the class, | |||
even if that object is <var>Null</var>: | |||
<p class="code">%sord = Descending(<i>itemFunction</i>) | |||
%sord = %(SortOrder for <i>itemType</i>):Descending(<i>itemFunction</i>) | |||
%sord = %sord:Descending(<i>itemFunction</i>) | |||
</p> | |||
'''Note:''' | |||
As shown in the second of these above, if you explicitly specify the | |||
class name, you must include the item datatype of the collection to be sorted, just as on a <var>SortOrder</var> object variable's [[SortOrder class#Declaring a SortOrder object variable|declaration]]. | |||
<li>For more information about the <var class="term">itemFunction</var> parameter, | <li>For more information about the <var class="term">itemFunction</var> parameter, | ||
see [[SortOrder class#Specifying a SortOrder's sort key method|"Specifying a SortOrder's sort key method"]]. | see [[SortOrder class#Specifying a SortOrder's sort key method|"Specifying a SortOrder's sort key method"]]. | ||
The parameter is a method value, not a <var class="product">User Language</var> expression, | |||
and you may not specify a function that itself has an argument. | and you may not specify a function that itself has an argument. | ||
In order to do this, see the [[SortOrder class#xmplocal|example using a Local function]], which shows a way to apply such a function in a sort. | In order to do this, see the [[SortOrder class#xmplocal|example using a Local function]], which shows a way to apply such a function in a sort. |
Latest revision as of 02:25, 3 April 2015
Create SortOrder object to sort in ascending or descending order (SortOrder class)
These shared methods create a new instance of the SortOrder class. The ordering direction of the new instance is given by the method name:
Ascending is low-to-high; Descending is high-to-low.
The sorting key of the new instance is specified by the input parameter to the method. This parameter is a function that gets applied to each item in the collection that is being sorted, and it must be a method that operates on the item type and returns a User Language intrinsic datatype (Float, String, Longstring, or Unicode) value.
Ascending/Descending syntax
%sortOrder = [%(SortOrder For itemType):]Ascending[( [itemFunction])]
%sortOrder = [%(SortOrder For itemType):]Descending[( [itemFunction])]
Syntax terms
%sortOrder | A SortOrder object variable to contain the new object instance. |
---|---|
[%(SortOrder For itemType)] | This optional specification of the class in parentheses denotes a virtual constructor. See "Usage notes", below, for more information about invoking a SortOrder virtual constructor. |
itemFunction | A method value (a method name literal, a method variable, a class Variable, or even a method that returns a method value) for a method that operates on objects of the type specified on the declaration of the collection being sorted, and that returns a numeric or string value.
The default itemFunction value as of Sirius Mods version 7.6 is the special value This, described further in the "Usage Notes," below. |
Usage Notes
- As described in "Virtual Constructor methods", Ascending and Descending can be invoked with
no method object, with an explicit class specification, or with an object variable of the class,
even if that object is Null:
%sord = Descending(itemFunction) %sord = %(SortOrder for itemType):Descending(itemFunction) %sord = %sord:Descending(itemFunction)
Note: As shown in the second of these above, if you explicitly specify the class name, you must include the item datatype of the collection to be sorted, just as on a SortOrder object variable's declaration.
- For more information about the itemFunction parameter, see "Specifying a SortOrder's sort key method". The parameter is a method value, not a User Language expression, and you may not specify a function that itself has an argument. In order to do this, see the example using a Local function, which shows a way to apply such a function in a sort.
- As of Sirius Mods version 7.6, the
default SortOrder itemFunction value is This, a
method value that is valid for User Language intrinsic method objects only.
The identity method This returns the value of the
method object to which it is applied.
The following statements are therefore equivalent:
%sortOrder = Descending(this) %sortOrder = Descending
Note: Using the default SortOrder above on a non-intrinsic method object produces a compilation error.