是否有可能进行排序取决于该个体对象的响应方法的对象列表? [英] Is it possible to sort a list of objects depending on if the individual object's response to a method?

查看:156
本文介绍了是否有可能进行排序取决于该个体对象的响应方法的对象列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示产品画廊,我包括既用于销售,而不是销售产品。只有我想的产品是出售给出现在列表的前面,而不是销售将出现在列表末尾的对象。

I am wanting to display a gallery of products where I include products that are both for sale and not for sale. Only I want the products that are for sale to appear at the front of the list and the objects that are not for sale will appear at the end of the list.

这是简单的方法对我来说,做到这一点是使两个列表,然后将它们合并(on_sale对象中的一个列表和一个列表不on_sale对象?):

An easy way for me to accomplish this is to make two lists, then merge them (one list of on_sale? objects and one list of not on_sale? objects):

available_products = []
sold_products = []
@products.each do |product|
  if product.on_sale?
    available_products << product
  else
    sold_products << product
  end
end

。 。 。但做了我现有的应用程序的结构,这将需要重构过量由于我的code的怪胎(我失去了我的分页,我宁愿不重构)。这将是更容易,如果有一种方法我的产品对象的现有列表进行排序模型的方法 on_sale?这返回一个布尔值。

. . . But do to the structure of my existing app, this would require an excessive amount of refactoring due to an oddity in my code (I lose my pagination, and I would rather not refactor). It would be easier if there were a way to sort the existing list of objects by my product model's method on_sale? which returns a boolean value.

是否有可能通过现有的列表更优雅迭代和在轨这个真或假的价值排序呢?我只问,因为有这么多,我不知道隐藏在这框架/语言(红宝石),我想知道如果他们的工作在我之前已经完成。

Is it possible to more elegantly iterate through an existing list and sort it by this true or false value in rails? I only ask because there is so much I'm not aware of hidden within this framework / language (ruby) and I'd like to know if they work has been done before me.

推荐答案

当然。理想情况下,我们会做这样的事情使用 sort_by !

Sure. Ideally we'd do something like this using sort_by!:

@products.sort_by! {|product| product.on_sale?}

或snazzier

or the snazzier

@products.sort_by!(&:on_sale?)

但遗憾的是,&LT; =&GT; 为布尔不起作用(见<一href="http://stackoverflow.com/questions/14816131/why-doesnt-sort-or-the-flying-saucer-operator-work-on-booleans-in-ruby">Why不排序或飞碟运营商(小于=&GT;)?在Ruby的布尔工作)和 sort_by 不为布尔值工作,所以我们需要使用这一招(感谢rohit89!)

but sadly, <=> doesn't work for booleans (see Why doesn't sort or the flying saucer operator (<=>) work on booleans in Ruby?) and sort_by doesn't work for boolean values, so we need to use this trick (thanks rohit89!)

@products.sort_by! {|product| product.on_sale? ? 0 : 1}


如果你想获得票友,在排序方法需要一个块,而该块里面,你可以用你喜欢的任何逻辑,包括类型转换和多键。尝试是这样的:


If you want to get fancier, the sort method takes a block, and inside that block you can use whatever logic you like, including type conversion and multiple keys. Try something like this:

@products.sort! do |a,b|
  a_value = a.on_sale? ? 0 : 1
  b_value = b.on_sale? ? 0 : 1
  a_value <=> b_value
end

或者这样的:

or this:

@products.sort! do |a,b|
  b.on_sale?.to_s <=> a.on_sale?.to_s
end

(把B前面一个,因为你要真实值来之前

或者如果你有一个辅助排序:

or if you have a secondary sort:

@products.sort! do |a,b|
  if a.on_sale? != b.on_sale?
    b.on_sale?.to_s <=> a.on_sale?.to_s
  else
    a.name <=> b.name
  end
end

注意排序返回一个新的集合,这通常是一个更清洁,不易出错的解决方案,但是的 排序! 修改原始集合的内容,你说的是一个要求。

Note that sort returns a new collection, which is usually a cleaner, less error-prone solution, but sort! modifies the contents of the original collection, which you said was a requirement.

这篇关于是否有可能进行排序取决于该个体对象的响应方法的对象列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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