Sorting an array is pretty simple, thanks to many helper classes you can sort not only by key but also by attributes (if you have array of objects).
Consider such array:

To sort it alphabetically just use:

and you will get such result:

Now consider array of objects, lets say it will be class called Person with attributes like firstName and lastName, to get it sorted by f.e. lastName use:

Isn’t it simple? But wait, there is more to come.
In many cases you want multiple sorting, so lets say sort persons by last name, then by first name. Here we come:

Example result will be:

Pretty cool, huh?

It seems to be trivial, but it’s not. If you want to use custom table and custom model, and sort collection by one of your table’s column, you can’t just use addAttributeToSort as you do with native Magento collection (like “catalog/product”). Instead of this, you should use setOrder, so your code should look like this: