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

Re: [ Natural Sort ]: Sorting a string with numbers

by blueflashlight (Pilgrim)
on Sep 15, 2005 at 18:31 UTC ( #492370=note: print w/replies, xml ) Need Help??


in reply to [ Natural Sort ]: Sorting a string with numbers

I love this site! I was writing my program, had a need for a routine that would do natural sorting, came right to the Monestary and Super Searched for "natural sort", and here I found this code, which worked almost perfectly!

Here is the one problem I have with it, and though I've read the write-up and follow-ups, and I think I get how it works, I can't seem to figure out a solution. The problem is this:

If the unsorted members of the list are something like ...
@list = ( 'apple1', 'apple', 'apple20' );

... then this routine will result in a sorted order of ...
apple1
apple20
apple

... but what I would expect (and want) would be ...
apple
apple1
apple20

I thought about going through my list beforehand, and appending something like "00000" to each member that didn't end in a numeric value, and then stripping the "00000" off after the sort, but that seems like a really stupid thing to do.

Can someone help me out please? Thanks!

Replies are listed 'Best First'.
Re^2: [ Natural Sort ]: Sorting a string with numbers
by QM (Parson) on Sep 15, 2005 at 20:33 UTC
    Did you do something wrong? I got what you expected:
    #!/your/perl/here use strict; use warnings; my @list = qw(apple1 apple apple20); my @sorted = @list[ map { unpack "N", substr($_,-4) } sort map { my $key = $list[$_]; $key =~ s[(\d+)][ pack "N", $1 ]ge; $key . pack "N", $_ } 0..$#list ]; print "@sorted\n";
    gives:
    apple apple1 apple20
    Post your code and let's check it.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      The whole program is a bit complex and does a bunch of stuff that isn't relavent to this issue, but I've cut out everything but this section that shows the problem I'm having.
      #!/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 "N", $_ } 0..$#h ]; foreach (@h_sorted) { print "$_\n"; };

      When I run this program, with my Perl (This is perl, v5.8.6 built for darwin-thread-multi-2level), I get:
      psapp psapp1 psapp2 psepapp1 psepapp2 psepapp psimgprod pspappuat pswebapp2 pswebapp
      The "psapp" items sort properly, but the "psepapp" and "pswebapp" ones show the problem I'm asking about. Thanks! -s-

        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        

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2021-05-10 08:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (105 votes). Check out past polls.

    Notices?