title: Partial sort Collection with limit and custom Comparator
body: <p>I want to sort an ArrayList called imageList like this:</p>
  
  <pre><code>Collections.sort(imageList, new MapComparator(Function.KEY_TIMESTAMP, "dsc"));
  </code></pre>
  
  <p>This works fine, but now I want to be able to set a limit (show only the newest 100 images, where the ArrayList is unsorted, so simply creating a sublist won't work) for performance reasons.</p>
  
  <p>My MapComparator class looks like this:</p>
  
  <pre><code>class MapComparator implements Comparator&lt;HashMap&lt;String, String&gt;&gt;
  {
      private final String key;
      private final String order;
  
      public MapComparator(String key, String order)
      {
          this.key = key;
          this.order = order;
      }
  
      public int compare(HashMap&lt;String, String&gt; first,
                         HashMap&lt;String, String&gt; second)
      {
          String firstValue = first.get(key);
          String secondValue = second.get(key);
          if(this.order.toLowerCase().contentEquals("asc"))
          {
              return firstValue.compareTo(secondValue);
          }else{
              return secondValue.compareTo(firstValue);
          }
  
      }
  }
  </code></pre>
  
  <p>Does anyone know how to implement that?
  Thanks in advance!</p>