Subject: Re: bug#22084: Potential Bug in sort -r

From: Assaf Gordon <[email-masked]>
Date: Thu, 3 Dec 2015 12:18:04 -0500

tag 22084 notabug
close 22084
stop

Hello Adrià and Deimos,

On 12/03/2015 07:30 AM, Adrià Rovira wrote:
> I noticed the reverse option is not correctly applied if it has to sort by more than one column.

This is not a bug, but simply a usage issue.
The "sort --help" page states:
" ... OPTS is one or more single-letter ordering options [bdfgiMhnRrV],
which override global ordering options for that key. "

That is,
1. If you use global sorting option (-n), and a key option WITHOUT
    ordering option (e.g. '-k1,1') - the global sorting option is in effect.

2. If you use *any* sorting option in the key specification
  (e.g. '-k1,1r'), it overrides the global sorting option,
  thus the order in effect is just 'reverse' (implying: alphabetical order).

These examples should demonstrate it:

     # Input data
     $ printf "1\n07\n2\n"
     1
     07
     2
     
     # Alphabetical (ascii) sort,
     # character "0" comes before "1"
     $ printf "1\n07\n2\n" | sort
     07
     1
     2
     
     # Numerical sort, value 7 comes after 2
     $ printf "1\n07\n2\n" | sort -n
     1
     2
     07
     
     # Global option (-n), key without ordering option:
     # numeric sort in effect
     $ printf "1\n07\n2\n" | sort -n -k1,1
     1
     2
     07
     
     # Global option (-n), key with any ordering option
     # (in this case 'b' = ignore leading blanks)
     # global numeric ordering is ignore
     $ printf "1\n07\n2\n" | sort -n -k1b,1
     07
     1
     2
     
     # Adding the numeric ordering to the key -
     # takes effect
     $ printf "1\n07\n2\n" | sort -n -k1bn,1
     1
     2
     07

regards,
  - Assaf