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?
我在Ruby on Rails中有一个对象数组。我想通过对象的属性对数组进行排序。是可能的吗?
9 个解决方案
#1
159
I recommend using sort_by instead:
我建议改用sort_by:
objects.sort_by {|obj| obj.attribute}
Especially if attribute may be calculated.
特别是如果可以计算属性。
#2
32
Yes, using Array#sort!
this is easy.
是的,使用数组排序号!这是很容易的。
myarray.sort! { |a, b| a.attribute <=> b.attribute }
#3
18
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
#4
14
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] }
#5
11
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 <=>(per)
@last_name + @first_name <=> per.last_name + per.first_name
end
end
Now an array of Person objects will be sortable on last_name.
现在Person对象数组可以在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" ]
#6
8
Array#sort works well, as posted above:
数组#sort工作得很好,如上所述:
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.
但是,您需要确保为该属性实现了<=>操作符。如果是Ruby原生数据类型,这不是问题。否则,编写自己的实现,如果a < b返回-1,如果它们相等则返回0,如果a > b返回1。
#7
7
More elegant objects.sort_by(&:attribute)
, you can add oon a .reverse
if you need to switch the order.
更优雅的对象。sort_by(&:属性),如果需要切换顺序,可以添加oon a .reverse。
#8
-1
Yes its possible
是的它的可能
http://ariejan.net/2007/01/28/ruby-sort-an-array-of-objects-by-an-attribute/
http://ariejan.net/2007/01/28/ruby-sort-an-array-of-objects-by-an-attribute/
#9
-1
@model_name.sort! { |a,b| a.attribute <=> b.attribute }
#1
159
I recommend using sort_by instead:
我建议改用sort_by:
objects.sort_by {|obj| obj.attribute}
Especially if attribute may be calculated.
特别是如果可以计算属性。
#2
32
Yes, using Array#sort!
this is easy.
是的,使用数组排序号!这是很容易的。
myarray.sort! { |a, b| a.attribute <=> b.attribute }
#3
18
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
#4
14
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] }
#5
11
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 <=>(per)
@last_name + @first_name <=> per.last_name + per.first_name
end
end
Now an array of Person objects will be sortable on last_name.
现在Person对象数组可以在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" ]
#6
8
Array#sort works well, as posted above:
数组#sort工作得很好,如上所述:
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.
但是,您需要确保为该属性实现了<=>操作符。如果是Ruby原生数据类型,这不是问题。否则,编写自己的实现,如果a < b返回-1,如果它们相等则返回0,如果a > b返回1。
#7
7
More elegant objects.sort_by(&:attribute)
, you can add oon a .reverse
if you need to switch the order.
更优雅的对象。sort_by(&:属性),如果需要切换顺序,可以添加oon a .reverse。
#8
-1
Yes its possible
是的它的可能
http://ariejan.net/2007/01/28/ruby-sort-an-array-of-objects-by-an-attribute/
http://ariejan.net/2007/01/28/ruby-sort-an-array-of-objects-by-an-attribute/
#9
-1
@model_name.sort! { |a,b| a.attribute <=> b.attribute }