[prev in list] [next in list] [prev in thread] [next in thread] 

List:       coreutils
Subject:    Re: sort:An unexpected result is displayed in the sort result.
From:       Pádraig_Brady <P () draigBrady ! com>
Date:       2021-11-05 15:21:21
Message-ID: 9b6d2faf-79da-59b2-78fc-bb7a7fd9260e () draigBrady ! com
[Download RAW message or body]

On 05/11/2021 09:31, Yang Yanchao wrote:
> Dear Coreutils Maintainers,
> 
> When I sort the use cases using "sort":
> [root@localhost test]# cat test
> 1.2.v1.6
> 1.0.v3.5
> 1.10.v2.4
> [root@localhost test]# sort -n -t'.' -k3,3 -k4,4 test
> 1.10.v2.4
> 1.0.v3.5
> 1.2.v1.6
> 
> The order I expected should be like this.
> 1.2.v1.6
> 1.10.v2.4
> 1.0.v3.5
> 
> It looks like the third column is alphabetic, and I added the -n option, which was \
> ignored, but I used -k to specify the third column as the first priority.This means \
> that -k has a lower priority than -n? Does this fit the intended design?

If you use the --debug option it shows -k3,3 is not matching
(due to not being numeric):

   $ printf '%s\n' 1.2.v1.6 | sort -n -t'.' -k3,3 -k4,4 --debug
   sort: text ordering performed using ‘en_IE.UTF-8' sorting rules
   1.2.v1.6
       ^ no match for key
          _

You probably want to restrict the numeric comparison to field 4 (with -k4,4n).
You probably also want to use --version-sort on field 3
rather than lexicographically (with -k3,3V), so that v10 sorts after v9:

$ printf '%s\n' 1.2.v10.6 1.0.v3.5 1.10.v2.4 |
   sort -t'.' -k3,3V -k4,4n --debug
1.10.v2.4
      __
         _
1.0.v3.5
     __
        _
1.2.v10.6
     ___
         _

cheers,
Pádraig


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic