GNU bug report logs - #30661
sort: add sort-by-hex-number feature

Previous Next

Package: coreutils;

Reported by: James Bunke <james_a_bunke <at> yahoo.com>

Date: Thu, 1 Mar 2018 00:02:01 UTC

Severity: wishlist

Full log


View this message in rfc822 format

From: Eric Blake <eblake <at> redhat.com>
To: James Bunke <james_a_bunke <at> yahoo.com>, "30661 <at> debbugs.gnu.org" <30661 <at> debbugs.gnu.org>
Subject: bug#30661: closed (Re: bug#30661: sort)
Date: Thu, 1 Mar 2018 19:52:24 -0600
reopen 30661
retitle 30661 RFE: Add way for sort to handle hex numbers
tag 30661 -notabug
thanks

On 03/01/2018 05:29 PM, James Bunke wrote:
> $ echo -e "170\n11" | sort -n

echo -e is not portable; printf is better.

> 11
> 170
> $ echo -e "AA\nB" | sort -n
> AA
> B
> $ echo -e "0xAA\n0xB" | sort -n
> 0xAA
> 0xB

Again, 'sort --debug' is your friend:

$ printf '0xAA\n0xB\n' | LC_ALL=C sort -n --debug
sort: using simple byte comparison
0xAA
_
____
0xB
_
___

The numeric sort key parses '0' and stops at 'x', because it does NOT 
parse hexadecimal.

Here's what POSIX has to say about -n:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sort.html

"-n
    Restrict the sort key to an initial numeric string, consisting of 
optional <blank> characters, optional <hyphen-minus> character, and zero 
or more digits with an optional radix character and thousands separators 
(as defined in the current locale), which shall be sorted by arithmetic 
value. An empty digit string shall be treated as zero. Leading zeros and 
signs on zeros shall not affect ordering."

Which does not directly mention "decimal", but the mention of a radix 
character (as in '1.2' or '1,2', depending on locale) pretty much 
implies decimal, as radix characters are only output by printf when 
printing floating point values in a decimal format.

> 
> Perhaps its the documentation that is lacking as I find no reference to hexadecimal ineither the "man" or "info" on sort -- can it sort hexadecimal?

-n cannot.  You are correct that we could improve the info page to make 
it explicit that -n sorts based on decimal values.  You also raise a 
good point that it may be worth adding a new sorting option that sorts 
by hexadecimal.  Although the existing practice of 
decorate/sort/undecorate to [temporarily] convert hex into decimal 
before sorting is going to be more portable, being able to directly sort 
hex does seem like something that may be worthwhile.

> No information  on whatsort considers to be a "numeral" or expects hexadecimal to be represented. I was justattempting to skip extra processes to convert the data or to write my own sort process.
> Thank you for your efforts on my behalf. Do you know who handles the documentation?Maybe there is newer man/info than on this old machine.

The info documentation is part of coreutils.git, so you've reached the 
right place.  I'm going to reopen and retitle this bug to request the 
ability to do hex sorting.


-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org




This bug report was last modified 6 years and 320 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.