sort()

Sort an array in ascending order, optionally with a custom comparison function.

Signature

sort(array [, comparison_function])

Parameters

Returns

New sorted array

Examples

Sort numbers:

nums = [3, 1, 4, 1, 5, 9, 2, 6]
sorted = sort(nums)
print(sorted)                   // [1 1 2 3 4 5 6 9]

Sort strings:

words = ["banana", "apple", "cherry"]
sorted = sort(words)
print(sorted)                   // [apple banana cherry]

Descending order with custom function:

nums = [3, 1, 4, 1, 5]
function desc(a, b)
  return a > b
end
sorted = sort(nums, desc)
print(sorted)                   // [5 4 3 1 1]

See Also