seaver has asked for the wisdom of the Perl Monks concerning the following question:

Dear all,

my problem lies in the fact that the interpreter seems to ignore the second section, which is are substr of the values use in the first section:

foreach my $ref ( sort { $a cmp $b || substr($a,1) <=> substr($b,1) } keys %{$self->{'residues'}{$i}}){ print "$ref\t"; }
The $refs are simply letters in uppercase followed by numbers, id like to sort them by their letters, and then by the numbers. Here is a sample (but wrong) output:
A95 A96 A97 A98 A99 B1 B10 B11 B12 B13
If I dont include the '$a cmp $b', then the keys are sorted according to the numbers, so I know that the substr section is working, but it just doesn't seem to pass through the '||'...

cheers
Sam

Replies are listed 'Best First'.
Re: sorting using substrs of same string
by seaver (Pilgrim) on Sep 23, 2003 at 20:25 UTC
    Dear all,

    figured it out for myself, it was my last sentence that made me think about it more: "but it just doesn't seem to pass through the '||'..."

    Re-reading the definition of the comparison operators, they return '0' for equivalence and the two strings being compared, though sorted, were never going to be equal, hence sort never fell through to the next level.

    here's my solution:

    foreach my $ref ( sort { substr($a,0,1) cmp substr($b,0,1) || substr($a,1) <=> substr($b,1) } keys %{$self->{'residues'}{$i}}){ print "$ref\t"; }
    cheers
    Sam
      Cardboard Programmers are amazing, aren't they? :-)

      Well done.

      ------
      We are the carpenters and bricklayers of the Information Age.

      The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.