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??
All,
I have an AoA that I need to sort in a way that I can't think of an immediate solution to. Each element will be an anonymous array of a variable number of strings. The first sort criteria is easy as it is based on the number of elements in the array.
@list = sort {@$a <=> @$b} @list;
The secondary sort criteria is where it gets tricky. For elements that are an anonymous array of the same size, I want them to appear in ascending asciibetical order working from the last element to the front.
['blah', 'asdf', 'foo', 'bar'], ['two'], ['zzz', 'def', 'ghi'], ['one'], ['mmm', 'def', 'ghi'], ['qqq', 'xyz', 'aaa']
should end up sorted as
['one'], ['two'], ['qqq', 'xyz', 'aaa'], ['mmm', 'def', 'ghi'], ['zzz', 'def', 'ghi'], ['blah', 'asdf', 'foo', 'bar'],
Any ideas?

Update: In an effort to be more clear, here are the instructions I would give someone to sort them by hand.

  1. First order by the number of elements with fewest first
  2. Break the items into groups based on the number of elements
  3. For each group, order the items based on the last element
  4. To break ties with the last element, consider the 2nd to the last element
  5. To break ties with the nth element, consider the n-1th element
While in this case, I need ASCIIbetical order, I am interested in a more general solution.

Cheers - L~R


In reply to Custom Sort An AoA by Limbic~Region

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 studying the Monastery: (4)
As of 2024-04-25 07:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found