Sorting
sort returns a new array in ascending order. sort_by takes a comparator function for custom ordering. Both leave the original unchanged.
|
|
Strings sort lexicographically. |
|
|
|
Use sort_by for custom ordering. Sort by absolute value: |
|
Duplicates are preserved. |
|
Empty and single-element arrays are fine. |
|
$ pyr run sort.pyr [1, 2, 3, 5, 8, 9] [5, 2, 8, 1, 9, 3] ["apple", "banana", "cherry"] [9, 8, 5, 3, 2, 1] [1, -2, -3, 4] [1, 1, 2, 3, 3] [] [42] |