Sort an Array.
Syntax
Parameters
subject
- The Array to sort.reverse
- Whether to reverse the sort.false
(default) - subject
will be sorted from lowest to highest.true
- subject
will be sorted from highest to lowest.preserve
- Whether to preserve item keys.false
(default) - Keys will be re-assigned.true
- Keys will be preserved.byKey
- Whether to sort by item key.false
(default) - subject
will be sorted by item value.true
- subject
will be sorted by item key.safe
- Whether to use "safe" sorting.false
(default) - subject
will be sorted using quicksort, which is not "safe".true
- subject
will be sorted using a merge sort, which is "safe".method
- Sort method options.0
(default) - Compare items using standard comparison.1
- Compare items numerically.2
- Compare items as case-sensitive strings.3
- Compare items as case-insensitive strings.4
- Compare items as case-sensitive strings, using natural ordering.5
- Compare items as case-insensitive strings, using natural ordering.6
- Compare items as case-sensitive strings, according to locale.7
- Compare items as case-insensitive strings, according to locale.
Result
Returns subject
sorted in order according to various options.
Key value, type, and order do not matter - the Array does not have to be numerically indexed.
"Safe" sorting is where two identical items will retain their original order. Sometimes this is not important, and so the quickest sorting method can be used (which here is an enhanced version of quicksort). At other times, it is important for identical keys to retain their relative order, in which case the slightly slower (but safe) merge sort will be used.
For more information about comparison methods, see the compare()
function.