Rails, Ruby, how to sort an Array?
@list.sort_by{|e| e[:time_ago]}
it defaults to ASC, however if you wanted DESC you can do:
@list.sort_by{|e| -e[:time_ago]}
Also it seems like you are trying to build the list from @messages
. You can simply do:
@list = @messages.map{|m|
{:id => m.id, :title => m.title, :time_ago => m.replies.first.created_at }
}
ruby/rails sort an array of records based on an array of string
You can do as
users.sort_by { |u| priority.index(u.priority) || priority.size }
The above sorting is done, with the assumption that the below Array
will be sorted as per your need, will hold all uniq values. users
array then will use the index of the sorted array.
priority = ["Wednesday","Tuesday","Friday"]
index(obj) → int or nil
Returns the index of the first object in
ary
such that the object is==
toobj
. Returnsnil
if no match is found.
priority
array doesn't hold all weekdays, rather 3
. I thought, if any users has priority, which is not present in the priority
array, let those users be placed in the last array. Suppose, for any user there is a priority, "Sunday"
, then, that user will be given lowest priority. How ?
Look at the expression priority.index(u.priority) || priority.size
, now with above mentioned sample, priority.index("sunday")
gives nil
, and right hand side expression of the ||
will be evaluated, i.e. priority.size
, which 3
. That's how that user
will be moved to the tail of the array.
Rails, how to sort the array of active records by created_at
You can sort an array by using sort_by.
In your case, you could do the following:
@total.sort_by(&:created_at)
Update:
- Remove @all << @find_user and @all_entry << @find_referal_user
- Why? Because otherwise you will have an array with multiple arrays
- Replace @total with: @total = @find_user + @find_referal_user
- Why? Because @total will now only consists of one merged array with all objects, ready to get sorted through .sort_by(&:created_at).
How to sort a two-dimensional array (2d array) by date in ruby on rails?
You can sort 2d arrays with sort_by method: sort_by
sorts in ascending order by default. If you want it to be in descending order, just reverse the result.
array.sort_by{ |a| a.first }.reverse
Sorting of array of hash
@final_array = @final_array.sort_by { |x, y| x[:ItemSize].downcase }
This makes sure that the case you pass into sort_by
is all the same. It does not change the case of the ItemSize values.
Sort an Array of Strings by their Integer Values
I'll throw another method out there since it's the shortest way I can think of
a.sort_by(&:to_i)
Rails Sort Array By Two Columns
Use sort_by:
surveys.sort_by { |survey| [survey.length, survey.cost] }
If you want to control asc vs desc, just multiply by -1:
surveys.sort_by { |survey| [-1 * survey.length, survey.cost] }
This should work for both regular array of objects or ActiveRecord::Relation
Related Topics
Uninstall Ruby Version from Rbenv
Getting Attribute's Value in Nokogiri to Extract Link Urls
$Redis Global Variable with Ruby on Rails
Shared Variable Among Ruby Processes
Rbenv Can't Change Global Ruby Version
How to Test 'Create' Controller Actions
Ruby, Check If Date Is a Weekend
How to Download a CSV File in Ruby on Rails
Detecting Image Equality at Different Resolutions
How to Check If a Variable Is an Instance of a Class
Convert Datetime String to Utc in Rails
Ruby: What Does the Asterisk in "P *1..10" Mean
How to Remove Validation Using Instance_Eval Clause in Rails
Starting or Restarting Unicorn with Capistrano 3.X