156

I have an array of objects in Ruby on Rails. I want to sort the array by an attribute of the object. Is it possible?

9 Answers 9

266

I recommend using sort_by instead:

objects.sort_by {|obj| obj.attribute}

Especially if attribute may be calculated.

Or a more concise approach:

objects.sort_by(&:attribute)
5
  • 88
    Or the shorthand version: objects.sort_by(&:attribute)
    – Nikola
    Jul 26, 2012 at 13:57
  • 3
    even more shorter objects.sort_by &:attribute
    – jasin_89
    Aug 6, 2013 at 10:28
  • 2
    If you have problems with uppercase & lowercase letters being sorted separately, you can use objects.sort_by { |obj| obj.attribute.downcase } Sep 15, 2014 at 16:49
  • 1
    Any idea how this compares with sort! (e.g. speed, etc.)? Jul 11, 2015 at 0:32
  • Enumerable#sort_by docs Aug 9, 2018 at 14:53
37

Yes, using Array#sort! this is easy.

myarray.sort! { |a, b|  a.attribute <=> b.attribute }
8
  • Thnx buddy but it didn't work out for me i have an array of objects. In which one of the attribute of the object is created_at. I want to sort it with this field. so i did @comm_bytes.sort! {|a, b| a.created_at <=> b.created_at } but no luck for me can u help....??
    – Satyam Gautam
    May 19, 2009 at 11:25
  • 4
    Is there a created_at method to access the @created_at attribute? What kind of object is @created_at? Does it define <=>? What kind of errors are you getting? etc, etc, ad nauseum. In other words, we need more detail than "but no luck for me".
    – rampion
    May 20, 2009 at 13:19
  • it works if you do myarray = myarray.sort {...} without "!" Jan 22, 2016 at 13:56
  • @Doru Yes, that works too, but why would you do that? It’s less direct and less efficient. Use sort if you want to preserve the original and assign the result to a different object; otherwise, use the in-place variant, sort!. In fact, sort calls sort! internally, after copying the original object. Jan 22, 2016 at 15:23
  • 1
    @Doru There must be an error somewhere else in the code, I can 100% guarantee you that sort! will work just fine, and always (!) do the same thing as what you’ve written. Jan 22, 2016 at 15:29
37

Ascending order :

objects_array.sort! { |a, b|  a.attribute <=> b.attribute }

or

objects_array.sort_by{ |obj| obj.attribute }

Descending order :

objects_array.sort! { |a, b|  b.attribute <=> a.attribute }

or

objects_array.sort_by{ |obj| obj.attribute }.reverse
21

in case you need sorting by two attributes, where first one is more important then second (means taking in account second arguments only if first arguments are equal), then you may do like this

myarray.sort{ |a,b| (a.attr1 == b.attr1) ? a.attr2 <=> b.attr2 : a.attr1 <=> b.attr1 }

or in case of array of arrays

myarray.sort{ |a,b| (a[0] == b[0]) ? a[1] <=> b[1] : a[0] <=> b[0] }
0
19

You can make any class sortable by overriding the <=> method:

class Person

  attr_accessor :first_name, :last_name

  def initialize(first_name, last_name)
    @first_name = first_name
    @last_name = last_name
  end

  def <=>(other)
    @last_name + @first_name <=> other.last_name + other.first_name
  end

end

Now an array of Person objects will be sortable on last_name.

ar = [Person.new("Eric", "Cunningham"), Person.new("Homer", "Allen")]

puts ar  # => [ "Eric Cunningham", "Homer Allen"]  (Person objects!)

ar.sort!

puts ar  # => [ "Homer Allen", "Eric Cunningham" ]
1
  • This (overriding <=>) is what I was looking for, thanks. One suggestion, though: Instead of sorting by (abbreviating for space) last + first (with string concatenation), maybe sort by [last, first] (making an array -- and let ruby's sort work out the groupings under the covers). May not matter in most cases, but imagine the case of comparing "Stephen Mac", "Alice Mac", and "Billy MacGregor". With array-based sorting, the "Mac"s will stay together. With string-based, they get split up.
    – lindes
    Jan 25 at 12:55
12

More elegant objects.sort_by(&:attribute), you can add on a .reverse if you need to switch the order.

10

Array#sort works well, as posted above:

myarray.sort! { |a, b|  a.attribute <=> b.attribute }

BUT, you need to make sure that the <=> operator is implemented for that attribute. If it's a Ruby native data type, this isn't a problem. Otherwise, write you own implementation that returns -1 if a < b, 0 if they are equal, and 1 if a > b.

-2

Yes its possible

http://ariejan.net/2007/01/28/ruby-sort-an-array-of-objects-by-an-attribute/

-2
@model_name.sort! { |a,b| a.attribute <=> b.attribute }
2
  • 2
    There are plenty of identical answers posted in 2009. No need to add another one.
    – interjay
    Mar 6, 2013 at 13:47
  • 2
    Don't use sort when you are sorting objects that can't be directly compared. If you have to access attributes or do a computation to get the value to compare use sort_by. It will be much faster. Nov 8, 2013 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.