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_comparatorがありsort!ますか?

4

4 に答える 4

40

独自の を定義<=>し、 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]

Comparable を実際に含める必要はありませんが、 を定義した後に含めると、無料で追加の機能が得られます<=>

それ以外の場合、オブジェクトが既に実装されている場合は、ブロックでEnumerableを使用できます。sort<=>

いくつかの異なる比較を使用する別の方法は、ラムダを使用することです。これは、新しい 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 の観察に同意します。)

于 2011-01-07T03:47:11.840 に答える