Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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!


Update
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!

Update*2
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.


Comment on Re: sorting entires by date
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://318188]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2014-07-14 00:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (252 votes), past polls