It may be prudent to use an ActiveRecord query to do the sorting instead of sorting manually. Databases are good at this ;)