30

A具有以下比较器:

class A
  attr_accessor x

  def my_comparator(a)
    x**2 <=> (a.x)**2
  end
end

我想使用这个比较器对每个项目都属于 A 类的数组进行排序:

class B
  def my_method
    items.sort!(<how can I pass my_comparator here ?>)
  end
end

我应该如何传递my_comparatorsort!?

4

4 回答 4

40

定义您自己的<=>,并包括 Comparable。这是来自可比文档

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]

您实际上不必包含 Comparable,但如果您在定义<=>.

否则,如果您的对象已经实现,则可以将Enumerablesort与块一起使用<=>

使用几种不同比较的另一种方法是使用 lambdas。这使用新的 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]
于 2011-01-07T03:19:10.730 回答
23

这两个都应该工作:

items.sort_by! { |a| (a.x)**2 }
items.sort! { |a1,a2| a1.my_comparator(a2) }
于 2011-01-07T03:18:19.510 回答
5
items.sort!(&:my_comparator)

这在:my_comparator.to_proc内部调用,它返回一个块

proc {|x,y| x.my_comparator(y)}

从而将这个答案简化为 Ben Alpert 的答案。

(但我同意 Phrogz 的观察,如果这是课程的自然顺序,那么您应该改用 Tin Man 的答案。)

于 2011-01-07T03:47:11.840 回答
1

如果你想在不同的地方重用这些比较器,最好将它们定义为一个类,而不是每次都重写相同的 lambda 表达式。

这是基于 Java 对 Comparable 接口的实现:

module Comparator
  def compare(a, b)
    raise NotImplementedError, 'must implement this method'
  end

  def to_proc
    ->(a, b) { compare(a, b) }
  end
end

class LengthComparator
  include Comparator

  def compare(a, b)
    a.length <=> b.length
  end
end

class ReverseLengthComparator < LengthComparator
  def compare(a, b)
    -super
  end
end

您在#compare 方法中实现比较逻辑。然后你可以像这样使用这个类:array.sort(&MyCustomComparator.new). 它本质上归结为一个 lambda 表达式,但在我看来支持更多的可重用性。

于 2018-07-26T09:19:41.830 回答