Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Sorting into a Specific Order

by Anonymous Monk
on Aug 29, 2002 at 10:32 UTC ( [id://193723]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a array of hashes, of which one key and value pair represents the day of a month. eg '19' or '21' etc. Another pair contains 24 hour clock readings, eg. '00' '01'...'23'. I want to sort the whole structure by date, but not in chronological order. I want to have the data arranged in a preset order, eg data from the 21st, then from the 17th, then the 14th etc. This order would be stored in another array. I then want to sort them within each date, by hour by a normal ascending sort.

I have previously been using 'sort' to sort them both chronologicaly, can sort support this more specific type of search? Is there another function, or will I need to scratch write it?


Replies are listed 'Best First'.
•Re: Sorting into a Specific Order
by merlyn (Sage) on Aug 29, 2002 at 11:10 UTC
    Since your example was sketchy, I'll have to give you only an example:
    my @aoh = ( { first => 'fred', last => 'flintstone', age => 30 }, { first => 'wilma', last => 'flintstone', age => 26 }, { first => 'pebbles', last => 'flintstone', age => 3 }, { first => 'barney', last => 'rubble', age => 28 }, { first => 'betty', last => 'rubble', age => 24 }, { first => 'bammbamm', last => 'rubble', age => 2 }, { first => 'mr.', last => 'slate', age => 35 }, ); # The boss comes first! my %lastname_sortorder = qw( flintstone 2 rubble 3 slate 1 ); my @sorted = sort { $lastname_sortorder{$a->{last}} <=> $lastname_sortorder{$b->{last}} +or # primary is lastname sort order $a->{age} <=> $b->{age} # secondary is age } @aoh;
    The key here is to create an "ordering table" which is used for the comparison, then look up your non-linear sort key in this ordering table, and sort on that instead.

    -- Randal L. Schwartz, Perl hacker

      ++merlyn for showing me a new trick - I didn't know you could put an "or" in the sort routine to get a secondary sort. thanks!
        You should get a copy of the llama then. That "trick" is in there, along with some other stuff you likely don't know then. {grin}

        -- Randal L. Schwartz, Perl hacker

        update: I just noticed that this is my 3000th post. Nice that it also happened to be about the llama. {grin}
Re: Sorting into a Specific Order
by RMGir (Prior) on Aug 29, 2002 at 12:03 UTC
    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"; }


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://193723]
Approved by htoug
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-18 17:23 GMT
Find Nodes?
    Voting Booth?

    No recent polls found