Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: sorting entires by date

by jweed (Chaplain)
on Jan 01, 2004 at 23:15 UTC ( #318188=note: print w/replies, xml ) Need Help??

in reply to sorting entires by date

If you want to sort a file by such a method, it would be best to read the file into an array (angle operator or Tie::File) and then use the Schwartzian Transform (read about it here) like this:
@file = map { $$_[0] } sort { $$a[1] <=> $$b[1] } map { [$_, (split /:/)[3] ] } @file;
Hope that helps!

Thought I'd make the ST clearer. Basically, you start with a map statement which takes every line in your file and puts it in an anonymous array with the timestamp (via split). Then, it passes an array with each of these to the sort routine, which sorts them based on the timestamp by getting that info from each anonymouss array in turn. Finally, the map statment at the end transforms this array of anonymous arrays back into an array with the lines from the file, properly sorted. Tada!

You could also try a GRT, which might look something like this:
@file = map { join ':', (split /:/)[1,2,3,0] } sort map { join ':', (split /:/)[3,0,1,2] } @file;
This has the clear limitation that it sorts asciibetically rather than numerically (doing it numerically might mitigate the benifits of GRT over ST, I'm not sure), which shouldn't be a problem really with time() but might be something to watch out for. Also, I'm sure this should have been done with pack or some such nonsense, but I don't know how. And finally, it may not even be faster. But, TMTOWTDI.

Who is Kayser Söze?
Code is (almost) always untested.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2018-04-27 05:09 GMT
Find Nodes?
    Voting Booth?