${(-)…}: Sort numerically

(-) sorts decimal integers numerically and supports a leading minus sign for negative numbers.

By using (n) instead, only positive integers are sorted correctly.

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

Examples

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