Anytime you can say "I want my data arranged so that...", all you need to do is find the comparison subroutine that expresses what you said. Since this isn't a standard routine, you're right, it has to be "scratch-written".
Of course, on occasion, the subroutine will need a bit of helper data. In this case, we need a mapping of days of month -> sort ordering.
# the initial array
my @AOH=(
{dayOfMonth=>19, clockReading=>02},
{dayOfMonth=>21, clockReading=>12},
{dayOfMonth=>15, clockReading=>04},
{dayOfMonth=>19, clockReading=>05},
{dayOfMonth=>15, clockReading=>23},
{dayOfMonth=>19, clockReading=>12}
);
# here's where you specify the ordering for the days of the
# month...
my %dayOfMonthMap=(
21 => 1,
17 => 2,
14 => 3,
15 => 4,
#...
19 => 21, # let's say
);
# For speed, let's remap that to an array, with the index
# being the day of the month
my @dayOfMonthRemap;
foreach(keys %dayOfMonthMap) {
$dayOfMonthRemap[$_]=$dayOfMonthMap{$_};
}
#ok, now we're ready to sort!
my @sortedArray=sort {
# if it's the same day, the || will make
# this compare by clockReading
$dayOfMonthRemap[$a->{dayOfMonth}] <=> $dayOfM
+onthRemap[$b->{dayOfMonth}] ||
$a->{clockReading} <=> $b->{clockReading}
} @AOH;
# Let's print out the result to make sure it's sane...
foreach(@sortedArray) {
print "$_->{dayOfMonth} : $_->{clockReading}\n";
}
--
Mike |