Hello,
I have an Array of Arrays.
I need to sort the entire array on the 12 field in each array. The field is a datefield, so newer date at the top and older date at the bottom.
I tried the normal sort functions and I cannot figure it out. Any help or pointing to docs on the web is much appreciated.
Thanks!
Jim
Write your own sort function that uses the ‘12 field’ in the comparison
Quicksort is a type of divide and conquer algorithm for sorting an array, based on a partitioning routine; the details of this partitioning can vary somewhat, so that quicksort is really a family of closely related algorithms. Applied to a range of at least two elements, partitioning produces a division into two consecutive non empty sub-ranges, in such a way that no element of the first sub-range is greater than any element of the second sub-range. After applying this partition, quicksort then r...
@AoA = sort { $a->[12] > $b->[12] } @AoA
An example sort function using the contents of the 12th (13th) item… but you didn’t say what your date format was. Use a CPAN Date module.
http://www.perlmonks.org/?node_id=667645
General docs are at perldoc.perl.org .
PS hubby wrote this, I’m not awesome enough to write my own sort functions in Perl : )
7stud
February 9, 2010, 4:39am
4
[b]
use strict;
use warnings;
use 5.010;
my @AoA = (
['a', '2003-11-09', 'b'],
['c', '2003-11-01', 'd'],
['e', '2002-11-01', 'f'],
['g', '2003-10-01', 'h'],
['g', '2002-10-01', 'h'],
);
my @new_arr = sort {$a->[1] cmp $b->[1]} @AoA;
for (@new_arr) {
say "@$_";
}
--output:--
g 2002-10-01 h
e 2002-11-01 f
g 2003-10-01 h
c 2003-11-01 d
a 2003-11-09 b
[/b]