Comment 2 for bug 1404548

Revision history for this message
rdks (rdks-deactivatedaccount) wrote :

This is not a bug, but expected behaviour.

If an ordering option is supplied to '-k' ('n' in this particular case),
global ordering options are overridden.

I.e. when sort is called this way:
    sort --reverse -k2
the global option '--reverse' takes effect.

But in this case:
    sort --reverse -k2n
the '--reverse' option is overridden by 'n', therefore the command
becomes equivalent to:
    sort -n -k2

The solution woud be to to either stick to global options:
   sort --reverse --numeric-sort -k2
or if sorting options for multiple fields are required, to add a
local reverse option to '-k':
   sort -k2rn

BTW: this behaviour is also documented in the man page:
...
       -k, --key=KEYDEF
              sort via a key; KEYDEF gives location and type
...
       KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a field number and C a character position in the field; both are origin 1, and the stop position defaults to the line's
       end. If neither -t nor -b is in effect, characters in a field are counted from the beginning of the preceding whitespace. OPTS is one or more single-letter ordering options [bdfgiMhnRrV], which
       override global ordering options for that key. If no key is given, use the entire line as the key.
...