Does your daily development have you jumping between client and server code like mine often does?

I recently needed to sort an array of “documents” from a NoSQL DB in PHP. So off to PHP.net to review the docs… there are 13 array sort functions in the table of choices. I decided on usort because my JSON was an array of objects – which is represented in PHP by an array of associative arrays – where I want to sort on a field of the child array. Note the description for usort explains it sorts by value using a user defined function, in my case the value is each child array and I want to sort on the Object-ID field.

Here is the JSON:

photos: [
   {
      Object-ID: 1,
      Location: "http://foo/x.jpeg"
   }, {
      Object-ID: 2,
      Location: "http://foo/y.png"
   }
]

The Javascript idiom for this would be:

photos.sort(function (p1, p2) {
   return p1['Object-ID'] - p2['Object-ID']
});

Please note I know the Object-ID field is an int but you should consider datatypes in your scenario.

The point of this story is my initial PHP solution just didn’t feel right. First of all you write your 1 line sort function and then supply a string of the name as the 2nd argument to usort. While not a big deal having a string match a function name and hoping over the lifetime of the code they never get out of sync nags at me.

The next irritant was my code was in a class so this syntax did not work (as an aside a static method does not work either) and as the docs point out (in example #3) you can pass an array with the class name AND the sort function name. Darn now we have two magic strings.

   usort($photos, array('MyClass', 'sortby_object-id'));

Then I ran into this great “Functional Programming in PHP” reference:
http://www.phptherightway.com/pages/Functional-Programming.html

Nice!

   usort($photos, function ($p1, $p2) {
      return $p1['Object-ID'] - $p2['Object-ID'];
   });

I will admit if you read far enough down on the PHP doc page you will see example #4 using a closure which might also get you to this syntax but never the less PHP: The Right Way is worth a look.