How to create a custom sort method in Ruby

This will give you ascending order for x then for y:

points.sort_by{ |p| [p.x, p.y] }

The best answer is provided by @AJcodez below:

points.sort_by{ |p| [p.x, p.y] }

The "correct" answer I originally provided, while it technically works, is not code I would recommend writing. I recall composing my response to fit the question's use of if/else rather than stopping to think or research whether Ruby had a more expressive, succinct way, which, of course, it does.


With a case statement:

ar.sort do |a, b|
  case
  when a.x < b.x
    -1
  when a.x > b.x
    1
  else
    a.y <=> b.y
  end
end 

With ternary:

ar.sort { |a,b| a.x < b.x ? -1 : (a.x > b.x ? 1 : (a.y <=> b.y)) }