I want to specify a custom block method to sort an object array by evaluating two properties. However, after many searches, I didn't find to any example without the <=> operator.
I want to compare a to 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
This is my code and it doesn't work:
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
This block is within a function return is exiting the function and returning -1.
<=>ruled out? Because you want to do it the hard way?endthe if block. Also you are sometimes usingb.yinstead ofb.x.