Chr*_*ier 37 ruby arrays sorting
我有一个对象数组,我需要通过一个可以是整数或零的位置属性进行排序,我需要将nil位置的对象放在数组的末尾.现在,我可以强制该位置返回一些值而不是nil,以便array.sort不会失败,但如果我使用0作为默认值,那么它将这些对象放在排序的前面.这种方法最好的方法是什么?我应该将nil值设置为一些"几乎"始终保证最终的可笑数字吗?或者是否有其他方法可以使array.sort方法将nil属性对象放在数组的末尾?代码如下所示:
class Parent
def sorted_children
children.sort{|a, b| a.position <=> b.position}
end
end
class Child
def position
category ? category.position : #what should the else be??
end
end
Run Code Online (Sandbox Code Playgroud)
现在,如果我把'else'变成1000000000,那么它很可能会把它们放在数组的末尾,但我不喜欢这个解决方案,因为它是任意的
gle*_*nra 104
我只想调整你的排序,把nil物品放在最后.尝试这样的事情.
foo = [nil, -3, 100, 4, 6, nil, 4, nil, 23]
foo.sort { |a,b| a && b ? a <=> b : a ? -1 : 1 }
=> [-3, 4, 4, 6, 23, 100, nil, nil, nil]
Run Code Online (Sandbox Code Playgroud)
这就是说:如果a和b都是非零的,那么它们通常会排序,但如果其中一个是零,则返回一个更大的状态.
gle*_*ald 16
我处理这样的事情:
children.sort_by {|child| [child.position ? 0 : 1,child.position || 0]}
Run Code Online (Sandbox Code Playgroud)
Bri*_*ell 15
如何在Child定义<=>基于category.positionif category存在的情况下,并且在没有category总是大于那些的情况下对项进行排序category?
class Child
# Not strictly necessary, but will define other comparisons based on <=>
include Comparable
def <=> other
return 0 if !category && !other.category
return 1 if !category
return -1 if !other.category
category.position <=> other.category.position
end
end
Run Code Online (Sandbox Code Playgroud)
然后在Parent你可以打电话children.sort.
公平地说,我对Ruby不是很熟悉,所以把它作为一个算法想法而不是代码一个...并重写?:运算符,因为Ruby有更清洁的东西.
你不能在比较中检查nil:
class Parent
def sorted_children
children.sort{|a,b|( a and b ) ? a <=> b : ( a ? -1 : 1 ) }
end
end
Run Code Online (Sandbox Code Playgroud)
编辑使用Glenra的代码,它实现了与我相同的功能,但代码更小(也可能更容易阅读).