title: How to create a custom sort method in Ruby
body: <p>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 <code>&lt;=&gt;</code> operator.</p>
  
  <p>I want to compare <code>a</code> to <code>b</code>:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>This is my code and it doesn't work:</p>
  
  <pre><code>ar.sort! do |a,b|
     if a.x &lt; b.y return -1
     elseif a.x &gt; b.x return 1
     else return a.y &lt;=&gt; b.y
  end
  </code></pre>
  
  <p>This block is within a function <code>return</code> is exiting the function and returning <code>-1</code>.</p>