If you wish to sort the records that match a query’s constraints, you can specify ordering criteria by calling
#order_by on the query match object.
Records can be sorted on any range index defined on the collection.
As an example, to get movies sorted oldest to newest:
movies.query do |movie| movie.order_by("year", :ASC) end
As you can see,
#order_by takes two arguments, the name of an index, and the ordering direction:
:ASC for “smallest values first”, and
:DESC for “largest values first”.
You can apply multiple ordering criteria on a single query, by calling
#order_by multiple times:
movies.query do |movie| movie.order_by("year", :ASC) movie.order_by("runningTime", :DESC) end
The above example will put older movies first, and then within a given year, the longest movies made that year will come before shorter movies of that same year.