Sort Numeric Array
# Treat $@ as a numeric array and echo it sorted.
function sort_numeric_array {
sort <(echo $@ | tr ' ' "\n") --numeric-sort | tr "\n" ' '
}
...
for x in {1..10}; do data[$x]=$(($x * $x * -1)
echo $(sort_numeric_array ${data[@]})
-100 -81 -64 -49 -36 -25 -16 -9 -4 -1
function sort_numeric_array {
sort <(echo $@ | tr ' ' "\n") --numeric-sort | tr "\n" ' '
}
...
for x in {1..10}; do data[$x]=$(($x * $x * -1)
echo $(sort_numeric_array ${data[@]})
-100 -81 -64 -49 -36 -25 -16 -9 -4 -1
Comments
Post a Comment