Blog entries tagged with "php"

Sorting in PHP is extra effort … and there is no spaceship

Monday, March 2nd, 2009 at 10:47 pm

Tonight I have been hacking in PHP on wp-lifestream in order to get grouped events ordered by time. I say hacking because there is a lot of PHP I don’t know and I don’t fully know how wp-lifestream is arranged.

When I first looked into sorting the grouped events my initial thought was to do it when the group was rendered. However that involved sorting data structures, so I decided that it would be easier to perform the sort when they were retrieved from the database. This is the change I made, and I thought it had worked.

Earlier today I found that while it worked when a feed was refreshed, it didn’t work when the group was updated after an event was deleted through the interface (I uploaded one too many photos to Flickr, which I later remved). So I had to turn back to the rendering code.

A var_dump() (Perl people should think Data::Dumper) later I knew what I was dealing with: an array of associative arrays that represented each item in the group. The relevant keys being date (in seconds since epoch) and title.

If this were perl it would be an array of hashrefs and I would have sorted it like this:

@events =
    sort { $a->{'date'} <=> $b->{'date'} || lc $a->{'title'} cmp lc $b->{'title'} } 
        @events;

Not so in PHP:

  • PHP sort functions sort the array in place instead of returning a new list
  • The more advanced PHP sort functions take a callback to use instead of a block
  • I’m not using PHP 5.3 so there are no closures yet, but at least anonymous functions can be created with create_function()
  • PHP does not have a spaceship operator (<=> for numerical comparison), even though there are equivalents of cmp (strcmp, strcasecmp).

This is what I ended up with:

usort(
    $data,
    create_function('$a,$b', '
        if ( $a["date"] == $b["date"] )
        {
            return strcasecmp( $b["title"], $a["title"] );
        }
        return ( $b["date"] < $a["date"] ) ? -1 : 1;
    ')
);

To me the logic is all messed up. Because there is no <=>, it needs a == comparison first. In the usort examples this returned 0, but gave me a convenient place to call strcasecmp() for the secondary sort. But that is breaking one sort into two statements and mixing in the other sort. It works and appears to be the PHP way of doing things, but it looks wrong to me.

(And yes, create_function() takes a string. Shudder…)

Tagged with: , , , ,