在Ruby中按布尔值对对象排序

时间:2022-06-08 10:04:49

My apologies if this has been answered before or is obvious...did some searching here and on the Goog and couldn't find an answer.

如果以前已经回答过这个问题,或者很明显,我很抱歉......在这里和Goog上做了一些搜索并找不到答案。

I'm looking to sort an array of Providers by price and whether they are a preferred_provider? (true or false)

我想按价格对一系列提供商进行排序,以及它们是否是preferred_provider? (对或错)

For instance in array p of Providers...

例如,在Providers的数组p中......

p1.price == 1, p1.preferred_provider? == false
p2.price == 2, p2.preferred_provider? == true
p2.price == 3, p3.preferred_provider? == true

I would like to p.sort_by and get:

我想p.sort_by并得到:

[p2 p3 p1]

IAW

p.sort_by {|x| x.preferred_provider?, x.price }

does not work and gets...

不起作用,得到......

undefined method `<=>' for false:FalseClass

Any suggestions on better ways to approach this problem?

关于更好地解决这个问题的方法的任何建议?

2 个解决方案

#1


Most languages provide sort functions that accept comparators for this sort of thing. In Ruby, this is just array.sort:

大多数语言都提供排序函数,可以接受这类事物的比较器。在Ruby中,这只是array.sort:

p.sort {|a, b| if (a.preferred_provider? == b.preferred_provider?
               then a.price <=> b.price
               elsif a.preferred_provider?
                    1
               else -1
       }

#2


You could define a <=> on the Provider class to do what you want, and then sort using the Array.sort method (rather than Enumerable.sort_by). Here's a definition of <=> that I whipped up:

您可以在Provider类上定义<=>以执行所需操作,然后使用Array.sort方法(而不是Enumerable.sort_by)进行排序。这是我掀起的<=>的定义:

class Provider
  def <=>(other)
    if preferred_provider?
      if other.preferred_provider?
        @price <=> other.price
      else
        1
      end
    else
      if other.preferred_provider?
        -1
      else
        @price <=> other.price
      end
    end
  end
end

Then, if you have your array p, you could just do p_sorted = p.sort.

然后,如果你有你的数组p,你可以做p_sorted = p.sort。

(Note that I haven't tested this code, so there may be a few errors, but I think it serves to demonstrate the idea.)

(请注意,我没有测试过这段代码,所以可能会有一些错误,但我认为它可以证明这个想法。)

#1


Most languages provide sort functions that accept comparators for this sort of thing. In Ruby, this is just array.sort:

大多数语言都提供排序函数,可以接受这类事物的比较器。在Ruby中,这只是array.sort:

p.sort {|a, b| if (a.preferred_provider? == b.preferred_provider?
               then a.price <=> b.price
               elsif a.preferred_provider?
                    1
               else -1
       }

#2


You could define a <=> on the Provider class to do what you want, and then sort using the Array.sort method (rather than Enumerable.sort_by). Here's a definition of <=> that I whipped up:

您可以在Provider类上定义<=>以执行所需操作,然后使用Array.sort方法(而不是Enumerable.sort_by)进行排序。这是我掀起的<=>的定义:

class Provider
  def <=>(other)
    if preferred_provider?
      if other.preferred_provider?
        @price <=> other.price
      else
        1
      end
    else
      if other.preferred_provider?
        -1
      else
        @price <=> other.price
      end
    end
  end
end

Then, if you have your array p, you could just do p_sorted = p.sort.

然后,如果你有你的数组p,你可以做p_sorted = p.sort。

(Note that I haven't tested this code, so there may be a few errors, but I think it serves to demonstrate the idea.)

(请注意,我没有测试过这段代码,所以可能会有一些错误,但我认为它可以证明这个想法。)