title: Writing a correct Comparator
body: <p>I am trying to write a <code>Comparator</code> to compare two objects based on a certain category field that they contain. Being new to writing custom <code>Comparator</code>s, I am unable to sort them properly. My code is as follows:  </p>
  
  <pre><code>@Override
  public int compare(ParseObject event1, ParseObject event2) 
  {
      String cat1 = event1.getString(Fields.CATEGORY);
      String cat2 = event2.getString(Fields.CATEGORY);
      Date date1 = event1.getDate(Fields.EVENT_DATE);
      Date date2 = event1.getDate(Fields.EVENT_DATE);
      if(cat1.equals(category) &amp;&amp; !cat2.equals(category))
      {
          // If event1 matches the category and event2 doesnt
          // event1 is greater
          Log.v("COMPARING", cat1 + " with " + cat2 + ", " + cat1 + " is greater ");
          return 1;
      }
      else if(!cat1.equals(category) &amp;&amp; cat2.equals(category))
      {
          // If event1 doesnt match the category and event2 does
          // event1 is lesser
          Log.v("COMPARING", cat1 + " with " + cat2 + ", " + cat1 + " is lesser ");
          return -1;
      }
      else if(cat1.equals(category) &amp;&amp; cat2.equals(category))
      {
          // If both belong to the same category
          Log.v("COMPARING", cat1 + " with " + cat2 + ", " + cat1 + " is equal ");
          return 0;
      }
      else
      {
          // Else they both do not belong to the category
          // return 0
          return 0;
      }
  }  
  </code></pre>
  
  <p>I have used a simple two-bit binary table 00,01,10,11 to come up with the conditions. Is this the right way to write the comparator? Have I made any mistake by making the conditions too convoluted?  </p>
  
  <p><strong>Case1 Looking for "Workshop" Events:</strong> (Workshop events are pushed down the list)   </p>
  
  <p>Events that have "Workshop" category are greater.</p>
  
  <pre><code>07-23 16:08:47.399: V/COMPARING(17806): Workshops
  07-23 16:08:47.399: V/COMPARING(17806): Workshops with Workshops, Workshops is equal 
  07-23 16:08:47.399: V/COMPARING(17806): Fests  with Workshops, Fests  is lesser 
  07-23 16:08:47.399: V/COMPARING(17806): Fests  with Workshops, Fests  is lesser 
  07-23 16:08:47.399: V/COMPARING(17806): Fests  with Workshops, Fests  is lesser 
  07-23 16:08:47.399: V/COMPARING(17806): Workshops with Workshops, Workshops is equal 
  07-23 16:08:47.399: V/COMPARING(17806): Workshops with Workshops, Workshops is equal   
  </code></pre>
  
  <p><strong>Case2 Looking for "Fest" Events:"</strong> (No comparison at all)  </p>
  
  <pre><code>07-23 16:10:13.639: V/COMPARING(17806): Fests  
  </code></pre>
  
  <p>I am using <code>Collections.sort(listOfEvents,myComparator)</code>.</p>