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

OK Sorting is a pain in my ....
So here we go...

I have 4 flat files. Tab delimited data the 3rd and 4th tab is LastName then FirstName.
OK here is what I currently have that will sort by field 3...
my @sorted_list = map { $database_in[ $$_[0] ] } sort { $$a[1] cmp $$b[1] } map { [ $_, (split /\t/, $database_in[ $_ ])[3] ] }(0..@database_ +in-1);
Now... I need to also sort the array first by field 3 like it does but also by field 4...
I need Jayne Williams to come before Jeff Williams... Any suggestions??


-----------------------
Billy S.
Slinar Hardtail - Guildless
Datal Ephialtes - Guildless
RallosZek.Net Admin/WebMaster
Aerynth.Net Admin/WebMaster

perl -e '$cat = "cat"; if ($cat =~ /\143\x61\x74/) { print "Its a cat! +\n"; } else { print "Thats a dog\n"; } print "\n";'

Replies are listed 'Best First'.
Re: Sort Question (Again) :(
by davorg (Chancellor) on Oct 19, 2001 at 20:20 UTC
    my @sorted_list = map { $_->[0] } sort { $a->[1] cmp $b->[1] or $a->[2] cmp $b->[2] } map { [ $_, (split /\t/)[3,4] ] } @database_in;

    I made a few stylistic changes too :)

    Update: Removed $_ from split. Hofmator is right - it was an artifact from the refactoring :(

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

      ++davorg, I just wanted to suggest the same thing, including the same stylistic changes :). But where do you see the benefit of giving split the explicit $_, instead of simply (split /\t/)[3,4]?

      -- Hofmator

      HEY.. I found out why Jeff was coming before Jayne... And my god that can be a loaded answer or question... Anyways...
      The problem is Jeff has a space in front of his name... Now how would I say ignore the space in the front of all fields and then sort??


      -----------------------
      Billy S.
      Slinar Hardtail - Guildless
      Datal Ephialtes - Guildless
      RallosZek.Net Admin/WebMaster
      Aerynth.Net Admin/WebMaster

      perl -e '$cat = "cat"; if ($cat =~ /\143\x61\x74/) { print "Its a cat! +\n"; } else { print "Thats a dog\n"; } print "\n";'

        Change the split criteria to remove whitespace as well. Building on davorg's code:

        my @sorted_list = map { $_->[0] } sort { $a->[1] cmp $b->[1] or $a->[2] cmp $b->[2] } map { [ $_, (split /\t\s*/)[3,4] ] } @database_in;

        Blessed Be
        The Pixel

        See answer below.
      Just tried exactly what you put... and this is the order it gave me...

      Jeff Williams
      Jayne Williams
      Phil Williams

      Doesn't Jayne come before Jeff??

      -----------------------
      Billy S.
      Slinar Hardtail - Guildless
      Datal Ephialtes - Guildless
      RallosZek.Net Admin/WebMaster
      Aerynth.Net Admin/WebMaster

      perl -e '$cat = "cat"; if ($cat =~ /\143\x61\x74/) { print "Its a cat! +\n"; } else { print "Thats a dog\n"; } print "\n";'
        Yeah, but 'Wiliams' comes before 'Williams' ... :-)

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

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: Sort Question (Again) :(
by runrig (Abbot) on Oct 19, 2001 at 20:39 UTC
    my @sorted_list = map { $$_[0] } sort { $$a[1] cmp $$b[1] } map { [ $_, do{ my $tmp = join "\x00", (split /\t/)[3,4]; $tmp =~ s/(^|\x00)\s+/$1/g;$tmp } ] } @database_in;
    Update:With extra added condition of stripping leading spaces. Fixed. Can't have 'local $_' inside do{} block due to split using outer $_.
      You all rock :)
      Here is the code that sorts properly and gets rid of the stinking space..

      my @sorted_list = map { $$_[0] } sort { $$a[1] cmp $$b[1] } map { [ $_, do{ my $tmp = join "\x00", (split /\t/)[3,4]; $tmp =~ s/(^|\x00)\s+/$1/g;$tmp } ] } @database_in;
      Thank you all for this help :)

      Update: Change code cause it didn't work 100%... This code does though.. OK I am just using the code above now :) Thank you again :)


      -----------------------
      Billy S.
      Slinar Hardtail - Guildless
      Datal Ephialtes - Guildless
      RallosZek.Net Admin/WebMaster
      Aerynth.Net Admin/WebMaster

      perl -e '$cat = "cat"; if ($cat =~ /\143\x61\x74/) { print "Its a cat! +\n"; } else { print "Thats a dog\n"; } print "\n";'