Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Sorting Outline Numbers

by japhy (Canon)
on Aug 10, 2005 at 18:53 UTC ( [id://482717]=note: print w/replies, xml ) Need Help??


in reply to Sorting Outline Numbers

So long as the section numbers never go above 255, this should work fine:
my @sorted = map join(".", map ord, split //), sort map join("", map chr, split /\./), @data;
It turns "4.2.1" into a three-byte string made of characters \x04, \x02, and \x01. It compares the outline numbers as strings of this form, and then expands them back to their numeral form.

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Replies are listed 'Best First'.
Re^2: Sorting Outline Numbers
by hv (Prior) on Aug 11, 2005 at 01:52 UTC

    Using sprintf to recreate the numbers would be rather more efficient:

    my @sorted = map sprintf('%vd', $_), sort map join('', map chr, split /\./), @data;

    Hugo

      Wow, I forgot entirely about that. I eschew the whole version-format idiom for numbers.

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re^2: Sorting Outline Numbers
by salva (Canon) on Aug 11, 2005 at 12:42 UTC
    using Sort::Key it becomes even simpler (and faster?):
    use Sort::Key qw(keysort); @sorted = keysort { join('', map chr, split /\./) } @data;
      I'm assuming that's producing a Schwartzian Transform. The benefit of the Guttman-Rosler Transform (which I've employed) is that it uses 'sort' instead of 'sort { ... }'. However, speed really isn't an issue here (nor should it be).

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
        I'm assuming that's producing a Schwartzian Transform.

        No, Sort::Key is implemented in C so it doesn't have to use nasty tricks to avoid calling perl code inside the sorting algorithm. And it is faster than the GRT most of the times.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://482717]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-03-19 04:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found