🕵️♀️ Task
The .order
method lets you sort your collection of records by one or more columns. .order
can accept a Hash
, String
, or Arel
as an argument.
Most commonly, we pass a Hash
argument where the keys are the columns you want to sort by, and the value are either :asc
(for ascending order) or :desc
(for descending order).
LevelUp
0.0% complete
LevelUp
0.0% complete
id | title | year | duration | description | image | director_id |
---|---|---|---|---|---|---|
49 | The Departed | 2006 | 151 | An undercover state cop who... | http://ia.media-imdb.com/im... | 12 |
50 | M | 1931 | 99 | When the police in a German... | http://ia.media-imdb.com/im... | 34 |
52 | Dunkirk | 2017 | 106 | Allied soldiers from Belgiu... | https://m.media-amazon.com/... | 5 |
51 | Interstellar | 2014 | 169 | A team of explorers travel ... | https://m.media-amazon.com/... | 5 |
id | name | dob | bio | image |
---|
id | name | dob | bio | image |
---|---|---|---|---|
44 | Amanda Plummer | 1957-03-23 | The daughter of Christopher... | http://ia.media-imdb.com/im... |
45 | Eric Stoltz | 1961-09-30 | Born on September 30, 1961 ... | http://ia.media-imdb.com/im... |
46 | Bruce Willis | 1955-03-19 | Actor and musician Bruce Wi... | http://ia.media-imdb.com/im... |
47 | Ving Rhames | 1959-05-12 | Irving "Ving" Rhames was bo... | http://ia.media-imdb.com/im... |
48 | Phil LaMarr | 1967-01-24 | http://ia.media-imdb.com/im... | |
49 | Maria de Medeiros | 1965-08-19 | Maria de Medeiros is the ol... | http://ia.media-imdb.com/im... |
50 | Rosanna Arquette | 1959-08-10 | Rosanna Arquette has acted ... | http://ia.media-imdb.com/im... |
51 | Peter Greene | 1965-10-08 | Born in Montclair, New Jers... | http://ia.media-imdb.com/im... |
id | actor_id | movie_id | name |
---|---|---|---|
49 | 44 | 4 | Honey Bunny / Yolanda |
50 | 45 | 4 | Lance |
51 | 46 | 4 | Butch Coolidge |
52 | 47 | 4 | Marsellus Wallace |
53 | 48 | 4 | Marvin |
54 | 49 | 4 | Fabienne |
55 | 50 | 4 | Jody |
56 | 51 | 4 | Zed |
Order the records from the customers table by the name column, descending
Customer.all.order({ :name => :desc})
=> #<Customer::ActiveRecord_Relation:0x00007fa4341e1ee8>
Order the records from the products table by the release_date column, descending
Product.all.order({ :release_date => :asc })
=> #<Product::ActiveRecord_Relation:0x00007fa4341e1ee8>