Your question inspired me to whip up a function that I'm sure will come in handy in the future:
sort_array () { local v="$1[*]" IFS=$'\n'; read -d $'\0' -a "$1" < <(sort "${@:2}" <<< "${!v}"); }
Usage: sort_array NameOfArrayVariable [flags to pass to sort command]
With nameArr=("Leia", "Darth Vader", "Anakin", "Han Solo", "Yoda"):
calling sort_array nameArr will result in nameArr containing ("Anakin," "Darth Vader," "Han Solo," "Leia," "Yoda"); calling sort_array nameArr -r will result in nameArr containing ("Yoda" "Leia," "Han Solo," "Darth Vader," "Anakin,")
Also, just a heads up, when you declare or set an array in bash, you do not comma separate elements, as "Leia", is the same as "Leia," unless you set IFS to contain a comma.
This function also works well with integer arrays:
$ nums=()
$ for ((i=0; i<10; ++i)); do nums+=($RANDOM); done
$ echo "${nums[@]}";
22928 7011 18865 24027 18559 9037 3885 10873 32369 21932
$ sort_array nums -n
$ echo "${nums[@]}"
3885 7011 9037 10873 18559 18865 21932 22928 24027 32369
$ sort_array nums -nr
$ echo "${nums[@]}"
32369 24027 22928 21932 18865 18559 10873 9037 7011 3885
sort -rto reverse order.arr=($(printf '%s\n' "${nameArr[@]}" | sort -r))printf '%s\n' ${arr[@]}is just a good reminder that it will process all values one-by-one from the input string, replacing aforloop for (in this case), the same functionality. (Think how easy extending this to process 2 words at a time withprintfwould be). You can also go for the idea that the shortest code wins ;-) . Good luck.