title: Order and sort_by difference in Ruby on Rails ActiveRecord
body: <p>I am trying to sort my data according to timestamp field in my controller, note that the timestamp field may be null and may have some value. I wrote the following query.</p>
  
  <pre><code>@item = Item.sort_by(&amp;:item_timestamp).reverse
              .paginate(:page =&gt; params[:page], :per_page =&gt;5)
  </code></pre>
  
  <p>But this gives error when I have items that have time_timestamp field value as NULL, but following query works.</p>
  
  <pre><code>@item = Item.order(:item_timestamp).reverse
              .paginate(:page =&gt; params[:page], :per_page =&gt; 5)
  </code></pre>
  
  <p>Can anybody tell the difference between these two queries, and in which condition to use which one? </p>
  
  <p>And I am using order and reverse to get the latest items from the database, Is this the best way or there are other best ways to get the latest data from database in terms of performance?</p>