Here is a shot using map.

Wags ;)
=====================================================================

#!perl -w

foreach my $MyKey (sort {$a->[2] cmp $b->[2] || $a->[3] cmp $b->[3]} 
                   map{ [$_, split(/\|/,$_)]} <DATA>) {
   printf "%-s", $MyKey->[0];
 }                   
__DATA__
1|Animals|Dogs
2|Animals|Cats
3|Beach|Babes
4|Beach|Hunks
5|Sports|Football
6|Sports|Hockey
7|Advertisement|General
8||Ships
9||Cars
^------ Code ends here
Output:
9||Cars
8||Ships
7|Advertisement|General
2|Animals|Cats
1|Animals|Dogs
3|Beach|Babes
4|Beach|Hunks
5|Sports|Football
6|Sports|Hockey
-----Original Message-----
From: Troy Sniff [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, June 05, 2001 16:26
To: [EMAIL PROTECTED]
Subject: Multi column sort


I want to perform a multi column sort using one field and then the next.

Example, lets say I have a datafile that looks like:

1|Animals|Dogs
2|Animals|Cats
3|Beach|Babes
4|Beach|Hunks
5|Sports|Football
6|Sports|Hockey
7|Advertisement|General
8||Ships
9||Cars

I want to split on the | and sort by the second column and then by the
third.

My result should come out as:

8||Ships
9||Cars
7|Advertisement|General
2|Animals|Cats
1|Animals|Dogs
3|Beach|Babes
4|Beach|Hunks
5|Sports|Football
6|Sports|Hockey

You'll notice the second column is sorted and then after that the third
is sorted.

I haven't performed this kind of sort so any help is appreciated.

Troy


_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users

Reply via email to