All help topics · Link to this topic
Showing help on 'sort()'
Syntax: sort(LIST <list> [, LIST <keys>, INT <natural sort order?>, INT <reverse>]) => LISTSorts <list> either by <keys> or using the list itself. When sorting <list> by itself, you can use an empty list ({}) for <keys> to specify additional optional arguments.
When sorting by a non-empty keys list, the keys list must be the same length as <list>, or E_INVARG is raised. The values used for sorting must be homogeneous scalar values. Lists, maps, anonymous objects, and WAIFs cannot be sorted and raise E_TYPE.
If <natural sort order> is true, strings containing multi-digit numbers will consider those numbers to be a single character. So, for instance, this means that 'x2' would come before 'x11' when sorted naturally because 2 is less than 11. This argument defaults to 0.
If <reverse> is true, the sort order is reversed. This argument defaults to 0.
Examples:
Sort a list by itself:
>;sort({"a57", "a5", "a7", "a1", "a2", "a11"})
=> {"a1", "a11", "a2", "a5", "a57", "a7"}
Sort a list by itself with natural sort order:
>;sort({"a57", "a5", "a7", "a1", "a2", "a11"}, {}, 1)
=> {"a1", "a2", "a5", "a7", "a11", "a57"}
Sort a list of strings by a list of numeric keys:
>;sort({"foo", "bar", "baz"}, {123, 5, 8000})
=> {"bar", "foo", "baz"}