$set.sort()
$set.sort($comparator)
Sorts the set. The order of the sort can be reversed by the Comparison reversal.
| Name | Type | Description | Mandatory | Default |
|---|---|---|---|---|
| set | Set | A set of elements to be sorted. | yes | |
| comparator | Function | A comparator function that is used to sort the elements. The comparator function can be created from an access function, see Comparison function. | no | The elements are compared directly with each other. However, this is only useful for primitive values for which a canonical comparison is defined. Objects are not directly comparable with each other and therefore need a comparison function. A comparison function expects two arguments x and y. The result is a value less than, equal to, or greater than 0, if x is less than, equal to, or greater than y. |
Type: Set
The new sorted set set.
list(8, 10, 5, 6).sort()
Output: A set with the elements [5, 6, 8, 10].
list("z", "a", "f")
.sort(x -> y -> {
if ($x < $y,
-1,
if ($x > $y, 1, 0));
})
Output: A list with the elements ["a", "f", "z"].
This example could also be simplified with a Comparative value.