coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sort by hexadecimal number


From: Chris Dunlop
Subject: Re: sort by hexadecimal number
Date: Thu, 19 Nov 2020 10:32:06 +1100

Thanks for the info - having the info page specifically mention hex sorting would indeed be helpful, I wasn't aware -g can be used for this case.

I was actually wishing upon a star for a "hex sort" mode which doesn't require the 0x adornment as that means I could avoid pre/post processing my data, but possibly that use case is not common enough to warrent a specific mode. If I need to pre/post process anyway, I may well continue using the faster lexi sort with fixed width fields.

Cheers,

Chris

On Wed, Nov 18, 2020 at 02:04:43PM +0000, Pádraig Brady wrote:
On 17/11/2020 00:20, Chris Dunlop wrote:
Hi,

It would be useful to have 'sort' be able to sort by hexidecimal number.

This has been asked a few times.
Previously I noted on the list that:

 -g supports hex, though has overhead in float conversion.
 For example `sort -k2,2g` would sort on the second key if hex.
 Note if the numbers are fixed width, or can be prepended
 with 0 to a fixed width, and are of consistent case,
 then you can use simple textual comparison to do the sort.

I'll apply the attached to make this clear in the info docs.

cheers,
Pádraig

From c3a5280731ee096cb156d87eaa38c1d887490380 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <P@draigBrady.com>
Date: Wed, 18 Nov 2020 14:00:43 +0000
Subject: [PATCH] doc: mention that sort -g supports hex numbers

* doc/coreutils.texi (sort invocation): Mention explicitly
that --general-numeric-sort supports arbitrary format hex numbers,
but also mention that consistent case/width hex numbers can
be sorted faster with a standard sort.
---
doc/coreutils.texi | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 99f6d9fb8..5ac3745bd 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -4397,6 +4397,12 @@ Use this option only if there is no alternative; it is 
much slower than
@option{--numeric-sort} (@option{-n}) and it can lose information when
converting to floating point.

+You can use this option to sort hexadecimal numbers prefixed with
+@samp{0x} or @samp{0X}, where those numbers are not fixed width,
+or of varying case.  However for hex numbers of consistent case,
+and left padded with @samp{0} to a consistent width, a standard
+lexicographic sort will be faster.
+
@item -h
@itemx --human-numeric-sort
@itemx --sort=human-numeric
--
2.26.2





reply via email to

[Prev in Thread] Current Thread [Next in Thread]