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.
- First order by the number of elements with fewest first
- Break the items into groups based on the number of elements
- For each group, order the items based on the last element
- To break ties with the last element, consider the 2nd to the last element
- 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.
-
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.