Bash associative array sorting by value

You can easily sort your output, in descending numerical order of the 3rd field:

for k in "${!authors[@]}"
do
    echo $k ' - ' ${authors["$k"]}
done |
sort -rn -k3

See sort(1) for more about the sort command. This just sorts output lines; I don’t know of any way to sort an array directly in bash.

I also can’t see how the above can give you names (“Pushkin” et al.) as array keys. In bash, array keys are always integers.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)