Archive for October, 2008

Sorting in Amazon SDB

Tuesday, October 28th, 2008

In July, Amazon Web Services released the ability to sort on SimpleDB queries. Prior to that, there was no guarantee of what order your result sets would come back in.

The syntax is pretty straightforward, just add sort asc or sort desc to the end of your query. The catch is that you have to include the attribute you want to sort on in your query. So you can’t do something like this:


['UserID' = '{$userID}'] sort 'SaveDate' desc

Simple DB throws an error. What you need to do is artificially add your sort attribute to the end of the query, like so:


['UserID' = '{$userID}'] intersection ['SaveDate' > ''] sort 'SaveDate' desc

The UserID query will be unaffected and your results will be sorted by SaveDate.