in reply to How to sort a multidimensional array
Well, there's lots of ways of course!Jeff Goldblum Actor Mary Heartman Priest John Ericsson Mathmetician Tony Cisneros Chef How can I sort on Vocation?
First off, I don't see any multidimensional array in your example. I just see a list of data. So let's make it a real multidimensional array:
Ok, so now we have an array with 4 elements, each of which is a reference to an array of 3 elements. So now you want to sort on the Vocation, which is the third field. There are many approaches...personally I'm old school, and am a sworn Schwartzian Transform kind of guy:my @data = ( [ qw(Jeff Goldblum Actor) ], [ qw(Mary Heartman Priest) ], [ qw(John Ericsson Mathmetician) ], [ qw(Tony Cisneros Chef) ], );
What the heck does that do? Read it backwards:my @sorted = map { $_->[0] } # Line 4 sort { $a->[1] cmp $b->[1] } # Line 3 map { [ $_, $_->[2] ] } # Line 2 @data; # Line 1
my @data = ( [ qw(Jeff Goldblum Actor) ], [ qw(Mary Heartman Priest) ], [ qw(John Ericsson Mathmetician) ], [ qw(Tony Cisneros Chef) ], ); my @sorted = map { $_->[0] } # Line 4 sort { $a->[1] cmp $b->[1] } # Line 3 map { [ $_, $_->[2] ] } # Line 2 @data; # Line 1 for ( @sorted ) { printf "%s %s - %s\n", @{ $_ }; }
Hope this helps some,fappy@flux[16] perl /tmp/sortit Jeff Goldblum - Actor Tony Cisneros - Chef John Ericsson - Mathmetician Mary Heartman - Priest
~Jeff
|
---|