Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Re: Re: Re: Sorting by date

by sulfericacid (Deacon)
on Jan 06, 2004 at 10:40 UTC ( #319069=note: print w/replies, xml ) Need Help??

in reply to Re: Re: Re: Sorting by date
in thread Sorting by date

This is very interesting, though I can certainly admit I've always been afraid of looking into MAP and SORT (just the way it looks so complicated makes me cringe). One thing after looking over this a number of times that I still don't get is... You are taking an array and putting only the date inside, right? This seems to be what the original poster wanted anyway, but with mapping how are you supposed to draw back the rest of the %list's values?
A => '1.13.04:Record:A', B => '1.12.04:Record:B', C => '3.13.04:Record:C', D => '3.13.02:Record:D',
You are stripping apart the date into $_->[1], how would you find the Record or the letter afterwards? Let's say $_->[1] is 1.12.04 this time. How would you find Record and B and take that with it so you can use it later? Or is that data lost and only the date can be retrieved after you've mapped?

At this level of confusion, unless this question makes sense and can be answered without digging the hole any deeper, even attempting a project which requires mapping or sorting like this is just not likely to happen in the near future.

Thanks for your time, everyone.

"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"


Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Sorting by date
by Roger (Parson) on Jan 06, 2004 at 12:39 UTC
    That's why the sorted record I built has three parameters, normalized date, original date string, and a hash key. You retrieve the rest of the data with the hash key, which is $_->[2]. And also remember that how to optimize the storage/retrieval depends on what sort of output is required.

    The code is actually quite simple, really. The sorting part is based on the Schwartzian Transform, invented by merlyn.

    The following is the explanation of the sort, hope you might find it useful. ;-)
    my @sorted_date = map { [ $_->[1], $_->[0], $_->[2] ] } sort { $b->[1] <=> $a->[1] } map { my @n = $list{$_} =~ /^(\d+)\.(\d+)\.(\d+):/; my $d = sprintf "%02d%02d%02d", @n[2,0,1]; [join('.',@n), $d, $_] } keys %list; step 1: build a list of hash keys: keys %list; step 2: build a new array by: for each element of the list built in step 1, capture the MM, DD and YY values into @n with the @ary = $str =~ /pattern/ idiom, normalize the date into YYMMDD form, store the original date, normalized date and hash key in anonymous array step 3: do the sort on normalized dates step 4: just reshuffle the fields in sorted data.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://319069]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (1)
As of 2018-05-28 00:16 GMT
Find Nodes?
    Voting Booth?