title: How do I use Comparator to define a custom sort order?
body: <p>I want to develop a sorting demo for car list. I am using data table to display car list. Now actually I want to sort the list by car color. Here it is not sort by alphabetic order. I want to use my custom sorting order like Red car come first, then Blue, etc.</p>
  
  <p>For that I try to use Java <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html" rel="noreferrer"><code>Comparator</code></a> and <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html" rel="noreferrer"><code>Comparable</code></a> but it allows to sort in alphabetic order only.</p>
  
  <p>So, can any one guide me the way to implement the technique to use so that the sorting becomes faster. </p>
  
  <pre><code>class Car implements Comparable&lt;Car&gt;
  {
      private String name;
      private String color;
  
      public Car(String name, String color){
          this.name = name;
          this.color = color;
      }
  
      //Implement the natural order for this class
      public int compareTo(Car c) {
          return name.compareTo(c.name);
      }
  
      static class ColorComparator implements Comparator&lt;Car&gt; {
          public int compare(Car c1, Car c2) {
              String a1 = c1.color;
              String a2 = c2.color;
              return a1.compareTo(a2);
          }
      }
  
      public static void main(String[] args) {
          List&lt;Car&gt; carList = new ArrayList&lt;&gt;();
          List&lt;String&gt; sortOrder = new ArrayList&lt;&gt;();
  
          carList.add(new Car("Ford","Silver"));
          carList.add(new Car("Tes","Blue"));
          carList.add(new Car("Honda","Magenta"));
  
          sortOrder.add("Silver");
          sortOrder.add("Magenta");
          sortOrder.add("Blue");
  
          // Now here I am confuse how to implement my custom sort             
      }
  }
  </code></pre>