我有一个对象数组,我需要按位置属性进行排序,可以是整数或nil,我需要在数组末尾有nil位置的对象.现在,我可以强制位置返回一些值,而不是nil,这样array.排序不会失败,但如果我使用0作为默认值,那么它会将这些对象放在排序的前面.做这种事最好的方法是什么?我是否应该将nil值设置为某个"几乎"总是保证在末尾的高得离谱的数字?或者有没有其他方法可以导致array.排序方法将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
现在,如果我把'else'设为100000000,那么它很可能会把它们放在数组的末尾,但我不喜欢这个解决方案,因为它是任意的