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

Re^4: [ Natural Sort ]: Sorting a string with numbers (fix)

by tye (Sage)
on Sep 18, 2005 at 03:25 UTC ( #492976=note: print w/replies, xml ) Need Help??

in reply to Re^3: [ Natural Sort ]: Sorting a string with numbers
in thread [ Natural Sort ]: Sorting a string with numbers

Yeah, that's the problem I discussed elsewhere. Here's a simple fix:

#!/usr/bin/perl -w use strict; my @h = ( qw( psapp1 psapp psapp2 psepapp1 psepapp psepapp2 psimgprod pspappuat pswebapp2 pswebapp ) ); my @h_sorted = @h[ map { unpack "N", substr($_,-4) } sort map { my $key = $h[$_]; $key =~ s[(\d+)][ pack "N", $1 ]ge; $key . pack "CNN", 0, 0, $_ } 0..$#h ]; foreach (@h_sorted) { print "$_\n"; };

This inserts 5 nul bytes which prevents the problem. 1 nul byte is enough to fix this specific case. 4 fixes all cases except for the possibility of "x0" sorting before "x". 5 fixes all cases (provided you don't have nul bytes in your strings).

- tye        

Replies are listed 'Best First'.
Re^5: [ Natural Sort ]: Sorting a string with numbers (fix)
by blueflashlight (Pilgrim) on Sep 18, 2005 at 17:41 UTC
    You are a hero. Thank you so much ... I even understand your solution, which, in itself is amazing. :-)

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2021-05-18 08:19 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (176 votes). Check out past polls.