Limbic~Region has asked for the wisdom of the Perl Monks concerning the following question:
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.
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.
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.@list = sort {@$a <=> @$b} @list;
should end up sorted as['blah', 'asdf', 'foo', 'bar'], ['two'], ['zzz', 'def', 'ghi'], ['one'], ['mmm', 'def', 'ghi'], ['qqq', 'xyz', 'aaa']
Any ideas?['one'], ['two'], ['qqq', 'xyz', 'aaa'], ['mmm', 'def', 'ghi'], ['zzz', 'def', 'ghi'], ['blah', 'asdf', 'foo', 'bar'],
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
Cheers - L~R
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Custom Sort An AoA
by Corion (Patriarch) on Apr 01, 2014 at 16:10 UTC | |
by Limbic~Region (Chancellor) on Apr 01, 2014 at 17:09 UTC | |
Re: Custom Sort An AoA
by kennethk (Abbot) on Apr 01, 2014 at 16:34 UTC | |
by Limbic~Region (Chancellor) on Apr 01, 2014 at 17:28 UTC | |
by SuicideJunkie (Vicar) on Apr 01, 2014 at 17:43 UTC | |
by LanX (Saint) on Apr 01, 2014 at 18:42 UTC | |
by Limbic~Region (Chancellor) on Apr 02, 2014 at 12:11 UTC | |
by Limbic~Region (Chancellor) on Apr 01, 2014 at 17:54 UTC | |
by shmem (Chancellor) on Apr 02, 2014 at 08:20 UTC | |
| |
by kennethk (Abbot) on Apr 02, 2014 at 18:09 UTC | |
by SuicideJunkie (Vicar) on Apr 01, 2014 at 17:31 UTC | |
by kennethk (Abbot) on Apr 02, 2014 at 18:13 UTC | |
Re: Custom Sort An AoA
by davido (Cardinal) on Apr 01, 2014 at 18:30 UTC | |
Re: Custom Sort An AoA
by AnomalousMonk (Archbishop) on Apr 01, 2014 at 17:40 UTC | |
Re: Custom Sort An AoA
by johngg (Canon) on Apr 01, 2014 at 18:34 UTC | |
Re: Custom Sort An AoA
by wind (Priest) on Apr 01, 2014 at 20:28 UTC | |
Re: Custom Sort An AoA
by kcott (Archbishop) on Apr 02, 2014 at 04:01 UTC | |
Re: Custom Sort An AoA
by Anonymous Monk on Apr 01, 2014 at 16:08 UTC |
Back to
Seekers of Perl Wisdom