in reply to Sorting issues

Forgive my simple brain but am I missing the point?
#!/usr/bin/perl -w use strict; my @database = <DATA>; sub g($) {(split/\|/,shift)[$#_]} my @sorted = sort {g$a<=>g$b} @database; print @sorted; __DATA__ a|b|c|10 d|e|f|g|h|9 i|100 j|k|2 l|m|n|o|11

"Argument is futile - you will be ignorralated!"

Replies are listed 'Best First'.
Re: Re: Sorting issues
by blue_cowdawg (Monsignor) on May 28, 2001 at 02:26 UTC

    You forgot something: You need to a

    chomp @database;
    before the sort. And as merlyn points out this is a good application of the Schwartzian Transform.


    Peter L. BergholdSchooner Technology Consulting, Inc.
    Peter@Berghold.Netwww.berghold.net

      i purposely left out the chomp because it works without.

      ++ for you if you can tell me when the \n will screw up the sort.

      The fix to dump the newlines is: sub g($){(split/\||\n/,shift)[-1]};

      "Argument is futile - you will be ignorralated!"