This is a short post that describes one way to sort Windows Azure Table entities by timestamp when using the Windows Azure SDK for PHP. The problem boils down to sorting an array of objects by a timestamp property, so the information here is nothing that hasn’t been done before. However, after spending some time looking for a way to use a filter in the Windows Azure SDK for PHP API, I didn’t find one, so I’m hoping this post might save others some time. In the end, my solution simply uses the PHP array_multisort function.

To illustrate the problem and my solution, I’ll reuse some of the code I wrote in this post: Accessing Windows Azure Table Storage from PHP. The important piece in that post to keep in mind is that when you define a class that inherits from Microsoft_WindowsAzure_Storage_TableEntity, the Windows Azure SDK for PHP maps specially annotated properties to “columns” in a table that has the same name as the class name. So, the example class below maps to the Contacts table and the $Name, $Address, and $Phone properties map to “columns” in that table.

class Contact extends Microsoft_WindowsAzure_Storage_TableEntity
{
  /**
  * @azure Name
  */
  public $Name;


  /**
  * @azure Address
  */
  public $Address;


  /**
  * @azure Phone
  */
  public $Phone;
}

Also note that the Contact class inherits the $_partitionKey, $_rowKey, and $_timestamp properties, as well as the corresponding methods getPartitionKey(), getRowKey(), and getTimestamp().

With that class in place, I can execute a query like this to retrieve all contacts:

$tableStorageClient = new Microsoft_WindowsAzure_Storage_Table();

 
$contacts = $tableStorageClient->retrieveEntities("Contact", null, "Contact");

  • The first parameter in the call to retrieveEntities specifies the table from which entities are retrieved.
  • The second parameter is a filter for entities. Specifying null will retrieve all entities.
  • The last parameter specifies the object type entities are retrieved as.

To sort the entities by timestamp, I’ll use two functions (which I essentially copied from the comments in the array_multisort documentation):

function objSort(&$objArray,$indexFunction,$sort_flags=0)
{
     $indices = array();
     foreach($objArray as $obj)
     {
         $indeces[] = $indexFunction($obj);
     }
     return array_multisort($indeces,$objArray,$sort_flags);
}


function getIndex($obj)
{
     return $obj->getTimestamp();
}

Note: If you are using the Windows Azure SDK v3.0.0 or later, timestamps are returned as PHP DateTime objects. So, to for this sorting solution to work, you’ll need to alter the getIndex function slightly (to get the Timestamp property on a DateTime object):

function getIndex($obj)

      return $obj->getTimestamp()->getTimestamp();
}

Now I can sort the $contacts array like this:

objSort($messages,'getIndex');

That’s it! I love the comments in the PHP documentation. :-)

Hope this saves someone some time.

Thanks.

-Brian

Share this on Twitter