Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Thanks for the correction :)

Naw, the transform doesn't have to modify the sort key... its more general purpose than that (though that is an excellent use). It is just a good algorithm for sorting (array based) data that is more complicated (and or a lot larger) than a plain array. Recently, for instance, I had an array of hashes, and used the transform to sort the array by the contents of one of the hash items.

For instance, if we had a phone book or something, consisting of an array of hashes, each hash being an entry ($phonebook[0]{firstname} would access the firstname, for example) we could do the transform to sort by say... or something.

@sorted_phonebook = map {$_->[1]} sort {$a->[0] cmp $b->[0]} map {[$_{lastname},$_]} @phonebook;

Now, we could indeed do a more complex grabbing of the field we are sorting by and/or a more complex sort algorithm, one in which we needed to modify the key even, but the transform is useful for this case as well.

--awwaiid

In reply to RE: RE: RE: Sorting an Array? by awwaiid
in thread Sorting an Associative Array? by target

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 examining the Monastery: (5)
As of 2024-04-19 07:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found