Extending the answer from @AndrewSchulman, using -rn as a global sort option reverses all columns. In this example, authors with the same associative array value will be output by reverse order of name.
For example
declare -A authors
authors=( [Pushkin]=10050 [Gogol]=23 [Dostoyevsky]=9999 [Tolstoy]=23 )
for k in "${!authors[@]}"
do
echo $k ' - ' ${authors["$k"]}
done | sort -rn -k3
will output
Pushkin - 10050
Dostoyevsky - 9999
Tolstoy - 23
Gogol - 23
Options for sorting specific columns can be provided after the column specifier.
i.e.
sort -k3rn
Note that keys can be specified as spans. Here -k3
happens to be fine because it is the final span, but to use only column 3 explicitly (in case further columns were added), it should be specified as -k3,3
,
Similarly to sort by column three in descending order, and then column one in ascending order (which is probably what is desired in this example):
declare -A authors
authors=( [Pushkin]=10050 [Gogol]=23 [Dostoyevsky]=9999 [Tolstoy]=23 )
for k in "${!authors[@]}"
do
echo $k ' - ' ${authors["$k"]}
done | sort -k3,3rn -k1,1
will output
Pushkin - 10050
Dostoyevsky - 9999
Gogol - 23
Tolstoy - 23