${(n)…}: Sort numerically

(n) sorts decimal integers numerically.

By using (-) instead, decimal integers with a leading minus sign are properly sorted.

With an additional (O), the sort order is reversed: ${(nO)name}.

Examples

Sort integers
1
2
3
4
5
declare data=(100 1 2 3 10)
echo "Sorting positive integers: " ${(n)data}
data=(100 -1 2 3 -10)
echo "Unsupported negative integers: " ${(n)data}
echo "Supporting negative integers: " ${(-)data}
Output:
1
2
3
Sorting positive integers:  1 2 3 10 100
Unsupported negative integers:  -1 -10 2 3 100
Supporting negative integers:  -10 -1 2 3 100
BashSupport Pro is a Zsh IDE with support for ${(n)…}try it now!
© 2020–2024 Joachim Ansorg
Imprint
Privacy Policy
Legal