title: How to use a separate comparator class to sort an array list
body: <p>Yeah this is homework but I am very stuck on what is wanted. The comparator class should implement java.util.Comparator, and the second argument of the sort() method should be an instance of my Comparator class.</p>
  
  <p>So far my comparator class looks like:</p>
  
  <pre><code>import java.util.Comparator;
  
  public class Compare implements Comparator&lt;Rational&gt; {
  
      public int compare1(int a, int b){
          int z = 0;
  
          if(a &gt; b)
              z = 1;
          else if(b &gt; a)
              z = -1;
          return z;
      }
  
      @Override
      public int compare(Rational a, Rational b) {
          // TODO Auto-generated method stub
          return 0;
      }
  }
  </code></pre>
  
  <p>I have a separate method to sort the array but this method uses compareTo()</p>
  
  <pre><code>public static Collection&lt;Rational&gt; sort1(List&lt;Rational&gt; list){
      List&lt;Rational&gt; sortedList1 = new ArrayList&lt;Rational&gt;();
  
      for (int i=0;i &lt; list.size();i++) { //iterating through list
          Rational currentValue = list.get(i);
  
          int pos = sortedList1.size(); 
          for (int j=0;j&lt;sortedList1.size();j++) {
              int comparison = currentValue.compareTo(sortedList1.get(j)); //comparing
              //this is the right position if the currentValue is greater or equal 
              //to the sorted value at this position
              if(comparison &gt; 0 || comparison == 0){
                  pos = j;
                  break;
              }
          }
          sortedList1.add(pos, currentValue);
      }
      return sortedList1;
  }
  </code></pre>
  
  <p>What should I use this method but using my separate comparator class? I'm totally lost on what I should do.</p>
  
  <p>My Rational Class looks like: </p>
  
  <p>public class Rational implements Comparable {</p>
  
  <pre><code>    private int num;   // the numerator
      private int den;   // the denominator
  
      // create and initialize a new Rational object
      public Rational(int numerator, int denominator) {
          if (denominator == 0) {
              throw new RuntimeException("Denominator is zero");
          }
          int g = gcd(numerator, denominator);
          num = numerator / g;
          den = denominator / g;
  
      }
  
      // return string representation of (this)
      public String toString() {
          if (den == 1) {
              return num + "";
          } else {
              return num + "/" + den;
          }
      }
  
      // return (this * b)
      public Rational times(Rational b) {
          return new Rational(this.num * b.num, this.den * b.den);
      }
  
      // return (this + b)
      public Rational plus(Rational b) {
          int numerator = (this.num * b.den) + (this.den * b.num);
          int denominator = this.den * b.den;
          return new Rational(numerator, denominator);
      }
  
      // return (1 / this)
      public Rational reciprocal() {
          return new Rational(den, num);
      }
  
      // return (this / b)
      public Rational divides(Rational b) {
          return this.times(b.reciprocal());
      }
  
      /** ***********************************************************************
       * Helper functions
      ************************************************************************ */
      // return gcd(m, n)
      private static int gcd(int m, int n) {
          if (0 == n) {
              return m;
          } else {
              return gcd(n, m % n);
          }
      }
  
      /** ***********************************************************************
       * Test client
      ************************************************************************ */
      public static void main(String[] args) {
          Rational x, y, z;
  
          // 1/2 + 1/3 = 5/6
          x = new Rational(1, 2);
          y = new Rational(1, 3);
          z = x.plus(y);
          System.out.println(z);
  
          // 8/9 + 1/9 = 1
          x = new Rational(8, 9);
          y = new Rational(1, 9);
          z = x.plus(y);
          System.out.println(z);
  
          //  4/17 * 7/3 = 28/51
          x = new Rational(4, 17);
          y = new Rational(7, 3);
          z = x.times(y);
          System.out.println(z);
  
          // 203/16957 * 9299/5887 = 17/899
          x = new Rational(203, 16957);
          y = new Rational(9299, 5887);
          z = x.times(y);
          System.out.println(z);
  
          // 0/6 = 0
          x = new Rational(0, 6);
          System.out.println(x);
  
      }
  
      public int compareTo(final Rational a, final Rational b) {
          return b.compareTo(a);
      }
  
      @Override
      public int compareTo(Rational arg0) {
          // TODO Auto-generated method stub
          return 0;
      }
  }
  </code></pre>