On the command line, what is a "general" and "string" numeric value when sorting?

2

According to "man sort" there are two ways to sort numerically:

-g, --general-numeric-sort
          compare according to general numerical value

and

-n, --numeric-sort
          compare according to string numerical value

What's the difference between these "values"?

The example below has been insufficient to demonstrate it to me

$ cat numbers.txt
 1
 1.0
01
010
10

$ sort -n numbers.txt
01
 1
 1.0
010
10

$ sort -g numbers.txt
01
 1
 1.0
010
10

jalanb

Posted 2013-09-30T10:30:38.540

Reputation: 133

Answers

4

This is answered over on Stack Overflow:

What's the difference between --general-numeric-sort and --numeric-sort options in gnu sort

From the answer:

General numeric sort compares the numbers as floats, this allows scientific notation eg 1.234E10 but is slower and subject to rounding error (1.2345678 could come after 1.2345679), numeric sort is just a regular alphabetic sort that knows 10 comes after 1.

From the GNU sort manual:

Use [general numeric sort] only if there is no alternative; it is much slower than --numeric-sort (-n) and it can lose information when converting to floating point.

slhck

Posted 2013-09-30T10:30:38.540

Reputation: 182 472