11.47 sort()

This function returns a sorted copy of a given list value. Only the values are sorted, not the indexes. Therefore, a sorting of values is also possible for associative arrays (hash data type).

Sorting is carried out grouped by the data types of the value elements, ascending by the ordinal of the data types and values. When sorting texts or strings, comparison is always done using the string data type, which is more suitable for sorting.

Sorting can be controlled like this:

  • With the Reverse parameter the sort sequence can be inverted.
  • Through the SortType parameter the sort sequence of strings and texts can be influenced.

Definition

anyvalue sort
(
      anyvalue ListValue input
  { , boolean  Reverse  := false       input }
  { , enum     SortType := sort_binary input }
)

Parameters

anyvalue ListValue input
This parameter specifies the list value to be sorted.
boolean Reverse := false input
If this optional parameter is set to true, sorting is carried out in descending order. The default value is false, which corresponds to an ascending sort.
enum SortType := sort_binary input

This optional parameter only applies to item values that have the data type string or are temporarily converted to a string for sorting.

Value range

sort_binary (default)

Sorting based on the Unicode character code.

sort_linguistic

Sorting based on the language and region settings (locale) of the system.

Depending on the system settings and the set code page, upper and lower case as well as umlauts are taken into account.

Since this sort requires code page conversions, it is slower than sort_binary.

For Unix or Linux we recommend using the UTF8 locale.

See also

Documentation of locale, LC_CTYPE, LC_COLLATE, strcoll for your operating system.

Return value

Sorted collection of the same data type as the passed collection.

Fault behavior

The function call fails if the ListValue parameter is not a collection or another parameter has an invalid value.

Example

// UTF8
dialog D

on dialog start
{
  variable list List := [ "Äcker", "Bande", "Bäcker", "binden",
                          "Bund", "Aachen", "an" ];
  variable string S;

  foreach S in sort(List, sort_linguistic) do
    print S;
  endfor
  exit();
}

/* returns: Aachen Äcker an Bäcker Bande binden Bund */

See also

Built-in functions find(), keys(), values()