I want to sort all my locations depending on how many facebook likes they have. But facebook_likes
is not an integer
, it is a string
.
This is the rails code I use:
@locations = Location.order("facebook_likes ASC").all
Right now I get something like this:
How can I sort depending on the value, so that the location with the most checkins is on the top. Is this solution with using strings instead of integers flawed form the beginning?
thx for your help!
@locations = Location.all.sort { |a, b| b.facebook_likes.to_i <=> a.facebook_likes.to_i }
or
@locations = Location.all.sort_by { |a| -(a.facebook_likes.to_i) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With