Ruby Sort_By Twice

ruby sort_by twice

I would suggest putting the column you want to sort by into an array.

entries.sort_by { |l| [l.project.name, l.project.time] }

This will respect the natural sort order for each type.

Ruby sort by multiple values?

a.sort { |a, b| [a['foo'], a['bar']] <=> [b['foo'], b['bar']] }

Sorting record array twice while maintaining initial sort

The Active Record Query Interface guide, gives us the following info on ordering records.

4 Ordering

To retrieve records from the database in a specific order, you can use
the order method.

For example, if you're getting a set of records and want to order them
in ascending order by the created_at field in your table:

Customer.order(:created_at)
# OR
Customer.order("created_at")

You could specify ASC or DESC as well:

Customer.order(created_at: :desc)
# OR
Customer.order(created_at: :asc)
# OR
Customer.order("created_at DESC")
# OR
Customer.order("created_at ASC")

Or ordering by multiple fields:

Customer.order(orders_count: :asc, created_at: :desc)
# OR
Customer.order(:orders_count, created_at: :desc)
# OR
Customer.order("orders_count ASC, created_at DESC")
# OR
Customer.order("orders_count ASC", "created_at DESC")

Applying this to your issue you would end up with:

companies.where(industry: industries_queried).order(plan_id: :desc, name: :asc)

Sort an array and make it unique on multiple conditions - Ruby

you can use sort_by method and uniq and values_at

hashForAnimals.sort_by{ |a| a[:sortOrder] }.uniq{ |k| k.values_at(:animalCd, :animalType) }

# => [{:animalCd=>"Cow", :animalType=>"Carnivore", :sortOrder=>1}, {:animalCd=>"Rabbit", :animalType=>"Herbivore", :sortOrder=>2}, {:animalCd=>"Tiger", :animalType=>"Carnivore", :sortOrder=>3}, {:animalCd=>"Shark", :animalType=>"Carnivore", :sortOrder=>4}, {:animalCd=>"Cow", :animalType=>"Herbivore", :sortOrder=>5}, {:animalCd=>"Bear", :animalType=>"Omnivore", :sortOrder=>7}]

Ruby on Rails: how do I sort with two columns using ActiveRecord?

Assuming you're using MySQL,

Model.all(:order => 'DATE(updated_at), price')

Note the distinction from the other answers. The updated_at column will be a full timestamp, so if you want to sort based on the day it was updated, you need to use a function to get just the date part from the timestamp. In MySQL, that is DATE().

Sort Array of Arrays by length with tiebreaker

You can still do this with sort_by, you just need to realize that Ruby arrays compare element-by-element:

ary <=> other_ary → -1, 0, +1 or nil

[...]

Each object in each array is compared (using the <=> operator).

Arrays are compared in an “element-wise” manner; the first two elements that are not equal will determine the return value for the whole comparison.

That means that you can use arrays as the sort_by key, then throw in a bit of integer negation to reverse the sort order and you get:

a.sort_by { |e| [-e.length, -e.last] }

That will give you the [[4, 5, 6, 7], [1, 2, 9], [1, 2, 3]] that you're looking for.

If you're not using numbers so the "negation to reverse the order" trick won't work, then use Shaunak's sort approach.



Related Topics



Leave a reply



Submit