Quirrel4.53.0

types.Array.sortby

Method on every array
array.sortby(key: function): any

Sorts the array in place by the value that key returns for each element.

The binding carries no declaration string, so the VM cannot report parameter names. The names above are from this page; the types and attributes still come from the VM. The VM also cannot tell an optional parameter from a variadic tail here, so read the brackets and any trailing ... from the prose below, not from the signature.

Parameters

keyfunctionkey(value, [index], [array]), called once for each element, in order, before the sort; not called for an array of one element or none

Return value

This array.

Errors

Whatever key throws. Without a _cmp metamethod, a pair of keys that < cannot compare throws comparison between X and Y. The call moves no element before the sort is done, so after an error only what key wrote to the array has changed.

Notes

key gets exactly as many of the arguments listed above as it declares parameters for, and never more.

The keys are compared the way sort compares elements without a comparator: numbers by value, strings lexicographically, and an instance through its _cmp metamethod. The sort is stable: elements with equal keys keep their relative order.

arr.sortby(@(v) v.score) gives the same order as arr.sort(@(a, b) a.score <=> b.score), but it calls a script function n times in place of about n log2(n) times, so it is faster for all but the smallest arrays. For more than one element, each call holds 2 n slots for its keys and the order of the elements. The first merge that moves elements adds a buffer of n/2 slots, for a total of at most 2.5 n slots. An array of 32 elements or less, or one already sorted by its keys, holds only 2 n slots. Up to 32 elements, outside a metamethod, the slots are on the VM stack and no array is allocated. One element or none needs no extra slots.

key must not change the size of the array: that throws array resized during sort operation.

Example

examples/types/array/sortby.nut
let players = [
  { name = "ann", score = 30 },
  { name = "bob", score = 10 },
  { name = "cid", score = 20 },
  { name = "dan", score = 10 }
]
players.sortby(@(p) p.score)
println(", ".join(players.map(@(p) $"{p.name} {p.score}")))

let words = ["pear", "fig", "banana", "kiwi"]
words.sortby(@(w) w.len())   // equal lengths keep their order
println(", ".join(words))
Output:
bob 10, dan 10, cid 20, ann 30
fig, pear, kiwi, banana

See also

sortSorts the array in place.
sortby_reversedSorts the array in place by the value that key returns for each element, in descending order.
arrayclass index