0

I am trying to sort set of string elements in descending order but the result surprises me. I can see 7.1.1 comes before 7.10.1, not sure why.

$ cat sort.sh 
#!/bin/bash
for i in $( echo "5.2.0 5.1.2 7.1.1 7.10.1" | tr ' ' '\n' | sort -r )
do
  echo $i
done

Output:

$ ./sort.sh 
7.1.1
7.10.1
5.2.0
5.1.2

Expected output:

$ ./sort.sh 
7.10.1
7.1.1
5.2.0
5.1.2

Am i missing anything here? Any tips would be really helpful.

Thanks

2
  • Look at an ASCII table and where . and 0 are in it. Commented Nov 12, 2022 at 17:22
  • I think you want sort -rn Commented Nov 12, 2022 at 17:32

1 Answer 1

2

Sort uses the sorting rules specified by the current locale. See "Sort does not sort in normal order!" in GNU Coreutils FAQ. GNU sort (1) manual page contains this warning:

*** WARNING *** The locale specified by the environment affects sort order. Set LC_ALL=C to get the traditional sort order that uses native byte values.

If you specifically want to sort version numbers, see the -V command line option in GNU sort:

-V, --version-sort

natural sort of (version) numbers within text

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.