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

In reply to Re: Sorting into a Specific Order by RMGir
in thread Sorting into a Specific Order by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.