title: compareTo() and Collections.sort() Solution For Multiple Column Ordering (Ascending) question
body: <p>Am somewhat confused with Java's compareTo() and Collections.sort() behavior.</p>
  
  <p>I am supposed to sort a column in ascending order using compareTo() &amp; Collections.sort().</p>
  
  <p>My criteria is (if the same number occurs than please sort the next available column).</p>
  
  <p>(1) Document Number
  (2) Posting Date
  (3) Transaction Date
  (4) Transaction Reference Number Comparison</p>
  
  <p>Here's the code (which is executed in a calling method) that implements the Collection.sort() method:</p>
  
  <pre><code>public int compareTo(CreditCardTransactionDetail t) {
     int comparison = 0;
  
     int documentNumberComparison = this.getDocumentNumber().compareTo(t.getDocumentNumber());
     if (documentNumberComparison != 0) {
         comparison = documentNumberComparison;
     } 
     else {
         int postingDateComparison = this.getTransactionPostingDate().compareTo(t.getTransactionPostingDate());
         if (postingDateComparison != 0) {
             comparison = postingDateComparison;
         } 
         else {
             int transactionDateComparison = this.getTransactionDate().compareTo(t.getTransactionDate());
             if (transactionDateComparison != 0) {
                 comparison = transactionDateComparison;
             }
             else {
                 int transactionRefNumberComparison = this.getTransactionReferenceNumber().compareTo(t.getTransactionReferenceNumber());
                 LOG.info("\n\n\t\ttransactionRefNumberComparison = " + transactionRefNumberComparison + "\n\n");
                 if (transactionRefNumberComparison != 0) {
                     comparison = transactionRefNumberComparison;
                 }
             }
         }
      return comparison;
  }
  </code></pre>
  
  <p>Question(s):</p>
  
  <p>(1) Am I doing the right thing? When a comparison = 0, it returns as -2. Is this correct behavior because I always thought it to be between -1,0,1.</p>
  
  <p>(2) Should I be using the comparator?</p>
  
  <p>Happy programming...</p>