Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Jeff Goldblum Actor Mary Heartman Priest John Ericsson Mathmetician Tony Cisneros Chef How can I sort on Vocation?
Well, there's lots of ways of course!

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:

my @data = ( [ qw(Jeff Goldblum Actor) ], [ qw(Mary Heartman Priest) ], [ qw(John Ericsson Mathmetician) ], [ qw(Tony Cisneros Chef) ], );
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 @sorted = map { $_->[0] } # Line 4 sort { $a->[1] cmp $b->[1] } # Line 3 map { [ $_, $_->[2] ] } # Line 2 @data; # Line 1
What the heck does that do? Read it backwards:
  1. Line #1 is your original list
  2. Line #2 - For each element in the original list, create an anonymous array of two elements: The first element in the array is the element fed in from the original array. The second element is field 2, which is the value of Vocation, or what you wanted to sort by
  3. Line #3 - Sort those anonymous arrays by field 1 (Vocation)
  4. Line #4 - As those anonymous array come out of sort, remap them to the original element from the unsorted @data.
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", @{ $_ }; }
fappy@flux[16] perl /tmp/sortit Jeff Goldblum - Actor Tony Cisneros - Chef John Ericsson - Mathmetician Mary Heartman - Priest
Hope this helps some,

~Jeff


In reply to Re: How to sort a multidimensional array by HuckinFappy
in thread How to sort a multidimensional array by benperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (1)
As of 2024-04-16 19:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found