如何通过自定义比较器进行“排序"? [英] How to pass a custom comparator to "sort"?

查看:79
本文介绍了如何通过自定义比较器进行“排序"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A具有以下比较器:

class A
  attr_accessor x

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

我想使用此比较器对数组进行排序,其中每个项均为A类:

I would like to use this comparator to sort an array where each item is of class A:

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

如何将my_comparator传递给sort!?

推荐答案

定义您自己的<=>,并包含Comparable.这来自可比文档:

Define your own <=>, and include Comparable. This is from the 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,但是如果在定义<=>之后执行此操作,则可以免费获得额外的功能.

You don't actually have to include Comparable, but you get extra functionality for free if you do that after having defined <=>.

否则,您可以使用 Enumerable的sort 如果您的对象已经实现<=>,则使用一个块.

Otherwise, you can use Enumerable's sort with a block if your objects implement <=> already.

使用几种不同比较的另一种方法是使用lambda.这使用了新的1.9.2声明语法:

Another way to use several different comparisons is to use lambdas. This uses the new 1.9.2 declaration syntax:

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]

这篇关于如何通过自定义比较器进行“排序"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆