2

What is the difference between the two sort options -n and -g?

It's a bit confusing to have too much detail but not enough adequate documentation.

0

1 Answer 1

5

TL;DR

While -n will sort simple floats such as 1.234, the -g option handles a much wider range of numerical formats but is slower.

Also -g is a GNU extension to the POSIX specification.


From man sort, the relevant parts are:

     -g, --general-numeric-sort, --sort=general-numeric
             Sort by general numerical value.  As opposed to -n, this option
             handles general floating points.  It has a more permissive format
             than that allowed by -n but it has a significant performance
             drawback.

...

     -n, --numeric-sort, --sort=numeric
             Sort fields numerically by arithmetic value.  Fields are supposed
             to have optional blanks in the beginning, an optional minus sign,
             zero or more digits (including decimal point and possible thou-
             sand separators).

...

STANDARDS
     The sort utility is compliant with the IEEE Std 1003.1-2008 (``POSIX.1'')
     specification.

     The flags [-ghRMSsTVz] are extensions to the POSIX specification.

...

NOTES

...

     When sorting by arithmetic value, using -n results in much better perfor-
     mance than -g so its use is encouraged whenever possible.

However, the full documentation is provided by info and not man.

From 7.1 sort: Sort text files, the description/distinction is clearer:

‘-g’

‘--general-numeric-sort’

‘--sort=general-numeric’

Sort numerically, converting a prefix of each line to a long double-precision floating point number. See Floating point. Do not report overflow, underflow, or conversion errors. Use the following collating sequence:

  • Lines that do not start with numbers (all considered to be equal).
  • NaNs (“Not a Number” values, in IEEE floating point arithmetic) in a consistent but machine-dependent order.
  • Minus infinity.
  • Finite numbers in ascending numeric order (with -0 and +0 equal).
  • Plus infinity.

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

You can use this option to sort hexadecimal numbers prefixed with ‘0x’ or ‘0X’, where those numbers are not fixed width, or of varying case. However for hex numbers of consistent case, and left padded with ‘0’ to a consistent width, a standard lexicographic sort will be faster.

...

‘-n’

‘--numeric-sort’

‘--sort=numeric’

Sort numerically. The number begins each line and consists of optional blanks, an optional ‘-’ sign, and zero or more digits possibly separated by thousands separators, optionally followed by a decimal-point character and zero or more digits. An empty number is treated as ‘0’. The LC_NUMERIC locale specifies the decimal-point character and thousands separator. By default a blank is a space or a tab, but the LC_CTYPE locale can change this.

Comparison is exact; there is no rounding error.

Neither a leading ‘+’ nor exponential notation is recognized. To compare such strings numerically, use the --general-numeric-sort (-g) option.


A quick demonstration:

$ printf '%s\n' 0.1 10 1e-2 | sort -n
0.1
1e-2
10

$ printf '%s\n' 0.1 10 1e-2 | sort -g
1e-2
0.1
10
0

You must log in to answer this question.