Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Re: Re: Re: Re: Sorting by date

by Roger (Parson)
on Jan 06, 2004 at 12:39 UTC ( #319097=note: print w/replies, xml ) Need Help??

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

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://319097]
[Corion]: Discipulus: Yeah, I can see that reasoning :) But spending 5 minutes in the sun today quite improved my mood :)
[marto]: lazyweb Q: so an IE window apparently opened via JS with toolbars=no, all other IE instances closed. Click a URL in an email or whatever, and it doesn't open a new window or tab.
[marto]: does JS lock down windows created via to prevent this?

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2018-03-21 11:35 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (267 votes). Check out past polls.