Mis*_*hko 26 ruby sorting comparator
类A
具有以下比较器:
class A
attr_accessor x
def my_comparator(a)
x**2 <=> (a.x)**2
end
end
Run Code Online (Sandbox Code Playgroud)
我想使用这个比较器对每个项目属于A类的数组进行排序:
class B
def my_method
items.sort!(<how can I pass my_comparator here ?>)
end
end
Run Code Online (Sandbox Code Playgroud)
我应该怎样my_comparator
转到sort!
?
the*_*Man 36
定义自己的<=>
,包括Comparable.这来自Comparable doc:
class SizeMatters
include Comparable
attr :str
def <=>(an_other)
str.size <=> an_other.str.size
end
def initialize(str)
@str = str
end
def inspect
@str
end
end
s1 = SizeMatters.new("Z")
s2 = SizeMatters.new("YY")
s3 = SizeMatters.new("XXX")
s4 = SizeMatters.new("WWWW")
s5 = SizeMatters.new("VVVVV")
s1 < s2 #=> true
s4.between?(s1, s3) #=> false
s4.between?(s3, s5) #=> true
[ s3, s2, s5, s4, s1 ].sort #=> [Z, YY, XXX, WWWW, VVVVV]
Run Code Online (Sandbox Code Playgroud)
您实际上不必包含Comparable,但如果您在定义后执行此操作,则可以免费获得额外的功能<=>
.
否则,如果对象已经实现,则可以将Enumerablesort
与块一起使用<=>
.
使用几种不同比较的另一种方法是使用lambda.这使用新的1.9.2声明语法:
ascending_sort = ->(a,b) { a <=> b }
descending_sort = ->(a,b) { b <=> a }
[1, 3, 2, 4].sort( & ascending_sort ) # => [1, 2, 3, 4]
[1, 3, 2, 4].sort( & descending_sort ) # => [4, 3, 2, 1]
foo = ascending_sort
[1, 3, 2, 4].sort( & foo ) # => [1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
Sop*_*ert 20
这两个都应该工作:
items.sort_by! { |a| (a.x)**2 }
items.sort! { |a1,a2| a1.my_comparator(a2) }
Run Code Online (Sandbox Code Playgroud)
items.sort!(&:my_comparator)
Run Code Online (Sandbox Code Playgroud)
这会调用:my_comparator.to_proc
内部,返回一个块
proc {|x,y| x.my_comparator(y)}
Run Code Online (Sandbox Code Playgroud)
从而减少了对Ben Alpert答案的答案.
(但我同意Phrogz的观察,即如果这是课堂的自然顺序,那么你应该使用Tin Man的答案.)