title: How to sort an array of Integer and OpenStruct objects by a key of one of the OpenStruct Keys
body: <p>I am just learning Ruby and started working with arrays. I have an array that looks like:</p>
  
  <pre><code>easyStats = [&lt;SampleArray: @id=0, @stats=#&lt;OpenStruct total_sessions_played=244, total_sessions_lost=124&gt;&gt;, 
               &lt;SampleArray: @id=1, @stats=#&lt;OpenStruct total_sessions_played=204, total_sessions_lost=129&gt;&gt;, 
               &lt;SampleArray: @id=2, @stats=#&lt;OpenStruct total_sessions_played=2, total_sessions_lost=4&gt;&gt;]
  </code></pre>
  
  <p>I can sort the array by the id (Integer) attribute as such: </p>
  
  <pre><code>easyStats.sort_by(&amp;:id)
  </code></pre>
  
  <p>However, what I would like to do is sort the array by a key of the OpenStruct object, I have tried the following options with different errors:</p>
  
  <pre><code>easyStats.sort_by(&amp;:stats)
  </code></pre>
  
  <blockquote>
    <p>Error: comparison of OpenStruct with OpenStruct failed</p>
  </blockquote>
  
  <pre><code>easyStats.sort_by(&amp;:stats[:total_sessions_played])
  </code></pre>
  
  <blockquote>
    <p>Error: no implicit conversion of Symbol into Integer</p>
  </blockquote>
  
  <pre><code>easyStats[:stats].sort_by(&amp;:total_sessions_played)
  </code></pre>
  
  <blockquote>
    <p>Error: no implicit conversion of Symbol into Integer</p>
  </blockquote>
  
  <p>I have seen solutions on how to sort an array of just one OpenStruct object and I think I understand it - <a href="https://stackoverflow.com/questions/29856004/how-to-sort-a-ruby-array-by-two-conditions">How to sort a ruby array by two conditions</a></p>
  
  <p>Could someone show me how to sort by any key of the OpenStruct object in this example (Array with Integer and OpenStruct objects)? </p>