Topic: https://brettterpstra.com/2015/03/17/shell-tricks-sort-a-bash-array-by-length/
hide preview

What's next? verify your email address for reply notifications!

Robert Abramovitz 10y, 23h ago

A slightly shorter version is

for i in ${arr[@]} ; do echo ${#i}$'\t'${i}; done | sort -n| cut -f 2-

It replaces the print and awk with a loop and an echo. By using TAB as the field separator it is able to exploit the default behavior of sort and cut. I wouldn't say this is more elegant since it uses essentially the same algorithm.

hide preview

What's next? verify your email address for reply notifications!