Sort an associative array in awk Sort an associative array in awk arrays arrays

Sort an associative array in awk


Instead of asort, use asorti(source, destination) which sorts the indices into a new array and you won't have to copy the array.

Then you can use the destination array as pointers into the source array.

For your example, you would use it like this:

n=asorti(chr_count, sorted)for (i=1; i<=n; i++) {        print sorted[i] " : " chr_count[sorted[i]]}


you can use the sort command. e.g.

for ( i in data ) print i ":", data[i]  | "sort"


Note that asort() and asorti() are specific to gawk, and are unknown to awk. For plain awk, you can roll your own sort() or get one from elsewhere.