title: Java implementing comparator on ArrayList
body: <p>I'm quite new to Java so this is probably pretty straight forward question.</p>
  
  <p>I want to sort an <code>ArrayList</code> in the class <code>MediaLib</code> based on the natural order of a specified key. </p>
  
  <p>I can't work out how to use my comparator (<code>compareTo(MediaInterface, key)</code>) which is in the <code>Media</code> class. Whats the best way to go about this?</p>
  
  <pre><code>package assign1;
  
  import java.util.*;
  
  public class Media implements MediaInterface {
  
      private Map&lt;String, Object&gt; fields;
      private static int compare;
  
   public Media(String title, String format) {
       fields = new TreeMap&lt;String, Object&gt;();
       fields.put("title", title);
       fields.put("format", format);
   }
  
  
   public Object get(String key) {
    return fields.get(key);
   }
  
  
   public void put(String key, Object value) {
    fields.put(key, value);
   }
  
  
   public boolean hasKeywords(String[] words, boolean combineWithAND) {
       Collection&lt;Object&gt; values = (Collection&lt;Object&gt;) fields.values();
       int count = 0;
    int size = 0;
    for (String s: words) {
           for (Object o: values) {
                  String t = o.toString();
                  if (t.indexOf(s) &gt;= 0) {
                      count++;
                      break;
                  }
           }
           size++;
    }
    if ((count == 0 &amp;&amp; !combineWithAND) || (combineWithAND &amp;&amp; (count != size))) {
        return false;
    }
    return true;
   }
  
  
   public int compareTo(MediaInterface mi, String key) { //&lt;&lt;&lt;&lt;&lt;&lt;&lt;------calling this!!
    if (mi == null)
     throw new NullPointerException();
    Media m = (Media) mi;
    Comparable mValue = (Comparable) m.get(key);
    Comparable lValue = (Comparable) fields.get(key);
    if ((mValue == null) &amp;&amp; (lValue == null)){
        return 0;
    }
    if ((lValue == null)){
              return 1;
          }
    if ((mValue == null)){
              return -1;
          }
    return (lValue).compareTo(mValue);
   }
  
  
   @Override
   public int compareTo(MediaInterface mi) {
    if (mi == null)
     throw new NullPointerException();
    Media m = (Media) mi;
    Set&lt;String&gt; lSet = fields.keySet();
    if (compareTo(m, "title") != 0) {
        return compareTo(m, "title");
    }
    if (compareTo(m, "year") != 0) {
              return compareTo(m, "year");
          }
    for (String s: lSet) {
        if (compareTo(m, s) != 0) {
            return compareTo(m, s);
        }
    }
    return 0;
   }
  
  
   public boolean equals(Object object) {
    if (object == null)
     return false;
    if (!(object instanceof Media))
     return false;
    Media m = (Media) object;
    if (compareTo(m) != 0) {
        return false;
    }
    return true;
   }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>    package assign1;
  
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.Collections;
  import java.util.Comparator;
  
  public class MediaLib implements Searchable {
   private ArrayList&lt;MediaInterface&gt; media;
  
   public MediaLib() {
    media = new ArrayList&lt;MediaInterface&gt;();
   }
  
  
   @Override
   public void add(MediaInterface mi) {
    if (media.isEmpty()) {
        media.add(mi);
    }
    else {
        for (MediaInterface m: media) {
            if (mi.equals(m)) {
                return;
            }
        }
        media.add(mi);
    }
   }
  
  
   @Override
   public boolean contains(MediaInterface mi) {
       for (MediaInterface m: media) {
              if (mi.equals(m)) {
                  return true;
              }
          }
       return false;
   }
  
  
   @Override
   public Collection&lt;MediaInterface&gt; findByKeyword(String[] words, boolean combineWithAND) {
    Collection&lt;MediaInterface&gt; foundList = new ArrayList&lt;MediaInterface&gt;();
       for (MediaInterface mi: media) {
        if (mi.hasKeywords(words, combineWithAND)) {
            foundList.add(mi);
        }
    }
       return foundList;
   }
  
  
   @Override
   public Collection&lt;MediaInterface&gt; findByTitle(String str) {
       Collection&lt;MediaInterface&gt; foundList = new ArrayList&lt;MediaInterface&gt;();
          for (MediaInterface mi: media) {
              if ((mi.get("title")).equals(str)) {
                  foundList.add(mi);
              }
          }
          return foundList;
   }
  
  
   @Override
   public Collection&lt;MediaInterface&gt; getAllWithFormat(String formatName) {
       Collection&lt;MediaInterface&gt; foundList = new ArrayList&lt;MediaInterface&gt;();
          for (MediaInterface mi: media) {
              if ((mi.get("format")).equals(formatName)) {
                  foundList.add(mi);
              }
          }
          return foundList;
   }
  
   public Collection&lt;MediaInterface&gt; getAll() {
       Collection&lt;MediaInterface&gt; fullList = new ArrayList&lt;MediaInterface&gt;();
          for (MediaInterface mi: media) {
              fullList.add(mi);
          }
          return fullList;
   }
  
  
   @Override
   public void removeAllWithKeyword(String[] words, boolean combineWithAND) {
       Collection&lt;MediaInterface&gt; foundList = findByKeyword(words, combineWithAND);
       for (MediaInterface mi: foundList) {
           media.remove(mi);
       }
   }
  
  
   @Override
   public void removeAllWithFormat(String format) {
       Collection&lt;MediaInterface&gt; foundList = getAllWithFormat(format);
          for (MediaInterface mi: foundList) {
              media.remove(mi);
          }
   }
  
  
   @Override
   public void sort() {
       Collections.sort(media);
   }
  
  
   @Override
   public void sort(final String fieldName) {  
       Collections.sort(media, new Media.compareTo(MediaInterface, fieldName))  //&lt;&lt;&lt;&lt;&lt;--------Trying to call compareTo()
  
       }
   }
  
  
   public void parse(java.io.BufferedReader br) throws java.io.IOException {
       while(br.readLine()!= null) {
           Media mi = new Media(/n br.readLine(), br.readLine());
           while 
  
       }
   }
  }
  </code></pre>