I built this test script around your code:
#!/usr/local/bin/perl -w use strict; my @database = <DATA>; my @sorted=sort{my $one=substr($a,rindex($a,'|')); my $two=substr($b,rindex($b,'|')); ($one <=> $two) } @database; print @sorted; __DATA__ a|b|c|10 d|e|f|100 g|h|i|2
which produced the following output:
Argument "|10\n" isn't numeric in ncmp at tmp.pl line 9, <DATA> chunk +3. Argument "|100\n" isn't numeric in ncmp at tmp.pl line 9, <DATA> chunk + 3. Argument "|100\n" isn't numeric in ncmp at tmp.pl line 9, <DATA> chunk + 3. Argument "|2\n" isn't numeric in ncmp at tmp.pl line 9, <DATA> chunk 3 +. a|b|c|10 d|e|f|100 g|h|i|2
The warnings reveal the problem quite conveniently; the substr() is including the pipe character, so all the values being compared are numerically equal to zero and thus to each other.

You can easily fix this error by adding 1 to each rindex(). However, I would suggest making another change as well. You're doing a fair amount of work in the sort subroutine, which makes it inefficient. A Schwartzian Transform moves the work out of the sort sub:

my @sorted = map $_->[1], sort { $a->[0] <=> $b->[0] } map [ substr($_,rindex($_,'|')+1), $_ ], @database;

But, there's another option that would work well here, and should be even more efficient because it uses an optimized sort sub. This one is often called the Guttman-Rosler Transform:

my $width = 10; # must be at least as big as the longest field being +compared my @sorted = map substr($_, index($_, '|')+1), sort map sprintf("%0${width}d|%s", substr($_,rindex($_,'|')+1) +, $_), @database;
As you can see, this is very similar to the Schwartzian Transform, but the intermediate value is a string instead of an anonymous array.

In reply to Re: Sorting issues by chipmunk
in thread Sorting issues by Stamp_Guy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.