title: Realm.io Android best approach to get last 20 items from a table
body: <p>In a table of let say 100 items, which is the best approach to get the last 20 objects.</p>
  
  <p>One way I can think of is to load all the objects , reverse the array , create a new array and loop from the results for 20 times filling the new array and return it.</p>
  
  <p>Something like as follows :</p>
  
  <pre><code> public ArrayList&lt;DataObject&gt; getLastItems (int qty){
  
      RealmResults&lt;DataObject&gt;results = realm.where(DataObject.class).findAll();
  
      Collections.reverse(results);
  
      ArrayList&lt;DataObject&gt;arrayList = new ArrayList&lt;&gt;();
  
      for (int i = 0; i == qty; i++){
  
          arrayList.add(results.get(i));
  
      }
  
      return arrayList;
  }
  </code></pre>
  
  <p>Is there a better faster way to do this in android using realm.io ?</p>
  
  <p><strong><em>Update</em></strong></p>
  
  <p>this is so far how this is handled..</p>
  
  <pre><code>  public ArrayList&lt;DataObject&gt; getLastItems (int qty){
  
      RealmResults&lt;DataObject&gt;results = realm.where(DataObject.class).findAll();
      ArrayList&lt;DataObject&gt; arrayList = new ArrayList&lt;&gt;();
      for (int i = results.size(); i &gt; Math.max(results.size() - 20, 0) ; i--) {
          arrayList.add(results.get(i-1));
      }
  
      return arrayList;
  }
  </code></pre>