[Israel.pm] Sorting By MySQL DATETIME Fields?

Yuval Yaari yuval at windax.com
Wed Jun 16 07:24:07 PDT 2004


On Wed, 2004-06-16 at 16:50, Shlomi Fish wrote:
> On Wednesday 16 June 2004 12:14, Yuval Yaari wrote:
> > Hi,
> >
> > I had a query that used JOIN and took too much time (4 seconds).
> > I figured Perl could handle it much faster, and so it did.
> >
> > I am using 2 queries now.
> > At the end, I have an array of $sth->fetchrow_hashref() which I would
> > like to sort by date (since it's in 2 queries, I can't ORDER BY Date).
> >
> 
> Note that if you perform ORDER BY on the two queries separately, then you can 
> then merge both arrays by using the "merge" algorithm:
> 
> http://www.maths.abdn.ac.uk/~igc/tch/mx4002/notes/node40.html
> 
> It is possible there is a fast (hard-coded) implementation of this algorithm 
> in CPAN, which accepts a comparison routine similar to sort's.
> 
> The overall complexity of both choices is the same ( O(Nlog(N)) ).
> 
> As for your question, you should probably use the Schwartzian transform (IIRC 
> how it's called) to map the DATETIME field to a scalar that is easily 
> sortable (unless it's already easily sortable), and then sort the items based 
> on this scalar. This will look something like this:

This is a Schwartzian Transform:
http://perlmonks.thepen.com/9108.html
(Array refs are so much faster... I never cache using a hash...)
And the classic Schwartzian Transform is in one command :)

Anyway, I know how to sort and use a Schwartzian Transform (it's all
over my code!) but I was asking how to convert this into something
sortable.

I didn't think it's already sortable (and it is: s/[\s:-]/g, then use
<=> to compare).

Stupid question by me... Please ignore it :)

Oh, and you can bet I'll use the (classic, all time favorite)
Schwartzian Transform to do it! :p

  --Yuval




More information about the Perl mailing list