sort - sort variables

 

SYNOPSIS

<sort [flags] $var [ICASE|NUM] [DESC|ASC] [$var2 ...]>


DESCRIPTION
The sort function takes one or more variable arguments to be sorted together. If only one variable is given, the return value is given in $ret. Otherwise (more than one variable), the variables are sorted in place, and are treated like the columns of a table to be sorted by rows. The first variable is the primary key to order by. Rows that compare the same for this variable are sorted by the next variable; if the second's values are identical then the third is compared, etc. If all variables' values compare equal for a given pair of rows, the original relative row order is preserved. In version 6 and later, string comparisons use the current apicp stringcomparemode setting (here).

Several flags may appear after each variable, and apply only to the immediately preceding variable:

  • DESC Sort this variable in descending order, instead of the default ascending order.

  • ASC Sort this variable in ascending order, the default.

  • ICASE Ignore case when sorting this variable, and sort it alphabetically if it's non-varchar. Thus "Norway" is equal to "norway" with this flag, and the integer value 123 is less than 45.

  • NUM Sort numerically: the variable's values are treated as numbers (floating point). This is useful for sorting a varchar variable whose values are numbers, which would otherwise be sorted alphabetically.

In addition, there are several flags that may be given immediately before the first variable, as overall flags:

  • SHORTEST Sort the shortest variable's number of values. Ideally, all variables given to <sort> should have the same number of values, so that a "row" of values across the variables is never broken up. However, if one or more variables are shorter than others, this could cause a "gap" to appear in the shorter variables' arrays (e.g. if the last row became sorted first, there would be no last-row value for the short variables to replace the first).

    With the SHORTEST flag set, these gaps are avoided by only sorting the first N values, where N is the number of values of the shortest variable. Later values are unsorted. Prior to version 5.01.1189552000 20070911, this was the default. In later versions, LONGEST is the default.

  • LONGEST Sort the longest variable's number of values. The problem of unequal length variables is handled by sorting all rows, and allowing gaps to appear in shorter variables as needed. These gaps - when they are "internal" or between pre-existing values - are populated with default/empty values. Thus, a short variable may end up with extra values after the <sort> (but not necessarily as many as the longest var). In version 5.01.1189552000 20070911 and later, LONGEST is the default.

If old-style SHORTEST behavior is needed as a default for some reason, it can be restored by editing texis.ini and setting [Texis] Default Vortex Sort Shortest to a non-zero value. Note that this setting is applied at compile-time only.


DIAGNOSTICS
sort returns the sorted list in $ret if one variable is given; otherwise it returns nothing and the variables are sorted in place.


EXAMPLE

<$colors =  red      orange   yellow   green    blue     violet>
<$num    = "FF0000" "FFA500" "FFFF00" "00FF00" "0000FF" "EE82EE">
<sort $num DESC $colors>
<LOOP $colors $num>
  $colors $num
</LOOP>

The output, sorted by descending order of color values (e.g. brightness):

yellow FFFF00
orange FFA500
red FF0000
violet EE82EE
green 00FF00
blue 0000FF


CAVEATS
The sort function was added Aug. 23 1996. The SHORTEST and LONGEST flags were added in version 5.01.1189552000 20070911.

All variables given to sort should have the same number of values. If not, some values may be unsorted, or some variables may have additional values inserted. See the SHORTEST/LONGEST flags.

In version 6 and later, string comparisons use the current apicp stringcomparemode setting (here).

This is one of the few functions in Vortex that modifies its arguments without the $&var reference syntax.


SEE ALSO
uniq


Copyright © Thunderstone Software     Last updated: Dec 10 2018
Copyright © 2019 Thunderstone Software LLC. All rights reserved.