我想通过评估两个属性来指定一个自定义块方法来对ruby中的对象数组进行排序。然而,在谷歌搜索后,我没有得到任何示例没有< =>运算符。ruby中的自定义排序方法
这就是我想做的事: 比较A对B:
if a.x less than b.x return -1
if a.x greater than b.x return 1
if a.x equals b.x, then compare by another property , like a.y vs b.y
这是我的代码(小白在红宝石,对不起),它不工作...
ar.sort! do |a,b|
if a.x < b.y return -1
elseif a.x > b.x return 1
else return a.y <=> b.y
end
这个块在一个函数内,所以返回函数返回并返回-1 ...我会很感激这方面的帮助。
亲切的问候。
为什么不使用排序方法? – 2013-03-14 04:19:23
@Gashner我正在使用它,但我打算定制评估。 – alexserver 2013-03-14 05:32:36
为什么排除'<=>'?因为你想要这么做吗? – 2013-03-14 07:35:24