Sorting an array of objects in Ruby by object attribute?

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)

Yes, using Array#sort! this is easy.

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

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