title: Creating a comparator class for Java TreeSet
body: <p>I have created a comparator class for Java's TreeSet function that I wish to use to order messages. This class looks as follows</p>
  
  <pre><code>public class MessageSentTimestampComparer
  {
  /// &lt;summary&gt;
  /// IComparer implementation that compares the epoch SentTimestamp and MessageId
  /// &lt;/summary&gt;
  /// &lt;param name="x"&gt;&lt;/param&gt;
  /// &lt;param name="y"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  
  public int compare(Message x, Message y)
  {
      String sentTimestampx = x.getAttributes().get("SentTimestamp");
      String sentTimestampy = y.getAttributes().get("SentTimestamp");
  
      if((sentTimestampx == null) | (sentTimestampy == null))
      {
          throw new NullPointerException("Unable to compare Messages " +
                  "because one of the messages did not have a SentTimestamp" +
                  " Attribute");
      }
  
      Long epochx = Long.valueOf(sentTimestampx);
      Long epochy = Long.valueOf(sentTimestampy);
  
      int result = epochx.compareTo(epochy);
  
      if (result != 0)
      {
          return result;
      }
      else
      {
          // same SentTimestamp so use the messageId for comparison
          return x.getMessageId().compareTo(y.getMessageId());
      }
  }
  }
  </code></pre>
  
  <p>But when I attempt to use this class as the comparator Eclipse gives and error and tells me to remove the call. I have been attempting to use the class like this</p>
  
  <pre><code>private SortedSet&lt;Message&gt; _set = new TreeSet&lt;Message&gt;(new MessageSentTimestampComparer());
  </code></pre>
  
  <p>I have also attempted to extend the MessageSentTimestampComparer as a comparator with no success. Can someone please explain what I am doing wrong. </p>