title: Sorting of Arraylist using Collections.sort and comparetor
body: <p>The scenario is I have <strong>brand_name, model_no and price</strong> of mobile company and I have to sort it according to the <strong>brand_name</strong> of mobile. And I am using Collections.sort for natural sorting and comparetor for custom sorting but due to some issue this code is not compiling. Please any one can help me to fix this problem   </p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Comparator;
  public class ArrayListSortingExample {
     private static class SmartPhone implements Comparable {
      private String brand;
      private String model;
      private int price;
      public SmartPhone(String brand, String model, int price){
          this.brand = brand;
          this.model = model;
          this.price = price;
      }
      @Override
      public int compareTo(SmartPhone sp) {
          return this.brand.compareTo(sp.brand);
      }
      @Override
      public String toString() {
          return "SmartPhone{" + "brand=" + brand + ", model=" + model + ", price=" + price + '}';
      }
  }
  private static class PriceComparator implements Comparator{
      @Override
      public int compare(SmartPhone sp1, SmartPhone sp2) {
          return (sp1.price &lt; sp2.price ) ? -1: (sp1.price &gt; sp2.price) ? 1:0 ;
      }
  }
  public static void main(String... args) {
      SmartPhone apple = new SmartPhone("Apple", "IPhone4S",1000);
      SmartPhone nokia = new SmartPhone("Nokia", "Lumia 800",600);
      SmartPhone samsung = new SmartPhone("Samsung", "Galaxy Ace",800);
      SmartPhone lg = new SmartPhone("LG", "Optimus",500);
      ArrayList Phones = new ArrayList();
      Phones.add(apple);
      Phones.add(nokia);
      Phones.add(samsung);
      Phones.add(lg);
      Collections.sort(Phones);
      System.out.println(Phones);
      Collections.sort(Phones, new PriceComparator());
      System.out.println(Phones);
  }
  }
  </code></pre>
  
  <p>The Output should be like this:</p>
  
  <pre><code>[SmartPhone{brand=Apple, model=IPhone4S, price=1000}, SmartPhone{brand=LG, model=Optimus, price=500}, SmartPhone{brand=Nokia, model=Lumia 800, price=600}, SmartPhone{brand=Samsung, model=Galaxy Ace, price=800}]
  
  [SmartPhone{brand=LG, model=Optimus, price=500}, SmartPhone{brand=Nokia, model=Lumia 800, price=600}, SmartPhone{brand=Samsung, model=Galaxy Ace, price=800}, SmartPhone{brand=Apple, model=IPhone4S, price=1000}]
  </code></pre>
  
  <p>the error is </p>
  
  <pre><code>Exception in thread "main" java.lang.AbstractMethodError: employee.ArrayListSortingExample$SmartPhone.compareTo(Ljava/lang/Object;)I
  at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:290)
  at java.util.ComparableTimSort.sort(ComparableTimSort.java:157)
  at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
  at java.util.Arrays.sort(Arrays.java:472)
  at java.util.Collections.sort(Collections.java:155)
  at employee.ArrayListSortingExample.main(ArrayListSortingExample.java:37)
  </code></pre>