-1

shell script to sort numbers including decimal numbers

I tried the following script and list with decimal number not working.

arr=(10 8 -20 100 1.2)

echo "Array in original order" echo ${arr[*]}

for ((i = 0; i<5; i++)) do

for((j = 0; j<5-i-1; j++)) 
do

    if [ ${arr[j]} -gt ${arr[$((j+1))]} ] 
    then
         
        temp=${arr[j]} 
        arr[$j]=${arr[$((j+1))]} 
        arr[$((j+1))]=$temp 
    fi
done
done
echo "Array in sorted order :" echo ${arr[*]}

OUTPUT:

Array in original order 10 8 -20 100 1.2 ./sort.sh: line 17: [: 1.2: integer expression expected Array in sorted order : -20 8 10 100 1.2

i want the list to be as follow:

-20 1.2 8 10 100

Biffen
  • 6,249
  • 6
  • 28
  • 36
  • 1
    How is this different from [your previous question](https://stackoverflow.com/questions/66049811/shell-script-to-sort-numbers-including-decimal-numbers)? – Biffen Feb 05 '21 at 08:13
  • please see this thread which has the answer: https://stackoverflow.com/questions/9939546/comparison-of-integer-and-floating-point-numbers-in-shell-script – Krishna Chaurasia Feb 05 '21 at 08:18
  • @anadanramar : Please specify ths shell you are using. You tag it as shell, which would mean POSIX shell, but it is certainly **not** POSIX shell, because you are using an Array, and POSIX shell does not have arrays. BTW: If you want to sort floats, I suggest to use Zsh, which can do float arithmetic. – user1934428 Feb 05 '21 at 09:54
  • @user1934428, it can do float arithmetic, but it has no knowledge on how to do numeric sorting of arrays. If you are referring to `"${(@n)array}"` then this does not behave as you expect. – kvantour Feb 05 '21 at 11:39
  • 1
    Is this a homework question to implement a sorting algorithm, or are external binaries allowed (such as `sort`) – kvantour Feb 05 '21 at 11:39
  • 1
    @kvantour : Maybe we have some misunderstanding here, but for all that I know, POSIX shell neither has arrays nor floating point arithmetic. If you have different information, I would appreciate if you can provide me some link on that matter. – user1934428 Feb 06 '21 at 17:32
  • @user1934428 I was referring to zsh. While zsh has knowledge of floating-point arithmetic, zsh cannot sort an array numerically when it contains floats. Only when it contains positive integers. – kvantour Feb 06 '21 at 19:48
  • @kvantour : Zsh, like bash, does **not** have any sorting algorithm built in. You have to write your own. Of course you **can** use an external program, such as `sort`, for this purpose but then you are stuck to what this offers. This is **unrelated** to the shell itself. Since writing a sort program is no rocket science, I suggest you just implement your own sort-function, and then can provide any additional whistles and bells you need for it. – user1934428 Feb 07 '21 at 20:12
  • 1
    @user1934428 ZSH does have knowledge of sorting. Have a look at the glob-qualifiers `o` and `O` to sort arrays. – kvantour Feb 08 '21 at 08:24
  • @kvantour : Right, I forgot this. And I now believe to see the bug in the sorting code of the OP: He is doing a `if [ ${arr[j]} -gt ${arr[$((j+1))]} ]` to compare the numbers. The correct way would be `if (( arr[j] > arr[j+1] ))`, which would work with floats as well. IMO, the question should be reopened because of this. – user1934428 Feb 08 '21 at 08:46
  • @kvantour : I now remember that the OP required POSIX shell, so I see that my zsh solution does not make sense for him anyway. Therefore, reopening of course is not advisable. – user1934428 Feb 08 '21 at 08:52

1 Answers1

1

i want the list to be as follow: -20 1.2 8 10 100

  1. Output numbers one per line.
  2. Sort
  3. Join with space

$ arr=(10 8 -20 100 1.2)
$ printf "%s\n" "${arr[@]}" | sort -g | paste -sd' '
-20 1.2 8 10 100

Don't reimplement sort using shell - it's going to be incredibly slow. The -g option to sort may not be available everywhere - it's for sorting floating point numbers.

[: 1.2: integer expression expected Array

The [ command handles integers only. 1.2 has a comma, [ can't handle it. Use another tool. Preferable python.

${arr[$((j+1))]}

No need to use $(( - stuff inside [ is already expanded arithmetically. Just ${arr[j+1]}.

j<5-i-1

is strange condition. When i=4 then it's j<0 and loop will not run at all. Just iterate from i instead of up to i - ((j=i;j<5;++j)).

echo "Array in sorted order :" echo ${arr[*]}

Will print echo to output. Just echo "Array in sorted order : ${arr[*]}" or echo "Array in sorted order :" "${arr[@]}"

KamilCuk
  • 120,984
  • 8
  • 59
  • 111