linux poison RSS
linux poison Email

Bash Script: How to sort an array

Here is simple & dirty trick to sort any types of element in an array ...
Feel free to copy and use this script.

Source: cat

array=(a x t g u o p w d f z l)
declare -a sortarray

touch tmp
for i in ${array[@]}; do
        echo $i >> tmp
        `sort tmp -o tmp`

while read line; do
        sortarray=(${sortarray[@]} $line)
done < tmp
rm tmp

echo "Here is the new sorted array ...."
echo ${sortarray[@]}

Output: ./
Here is the new sorted array ....
a d f g l o p t u w x z


Post a Comment

Related Posts with Thumbnails