in reply to Sorting a slurped file by a date field
You're almost there. There are a few different approaches you can take to get the rest of the way.
The simplist is to store a reference to a list in the existing lists (thus creating an array-of-arrays. In this case, you wouldn't need to modify your current sort lines. However, it would change the layout of your datastructure, so other code will probably have to change.
The second way is to use a Schwarzian Transform, which pulls the data apart before it gets to sort and then puts it back together. It looks like this:
my @sortedComplete = map { $_->[0] } sort { $a->[0] <=> $b->[0] } map { [ $_, (split /\t/, $_)[3] ]} @complete; # Do the same for @progress
The third solution is the GRT, which is conceptually similar to the Schwartzian, but is a bit faster. The difference is that in teh Schwartzian, you pull the data into a seperate portion of a temperary data structure, but in the GRT, you keep everything in the same string. This means you can make use of Perl's built-in sorting subroutine, which is implemented in C and will likely be much faster than calling the Perl block. It looks something like this (totally untested):
my @sortedComplete = map { join "\t", (split/\t/)[1,2,3,0] } sort map { join "\t", (split/\t/)[3,0,1,2] } @complete; # Do the same for @progress
The trick here is that the field you want to sort on goes first in the string. Note, though, that if two dates are the same, they'll be sorted based on the characters following the date.
----
I wanted to explore how Perl's closures can be
manipulated, and ended up creating an object
system by accident. -- Schemer
: () { :|:& };:
Note: All code is untested, unless otherwise stated
|
|---|