Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Comparator to define a custom sort order?

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.

For that I try to use Java Comparator and Comparable but it allows to sort in alphabetic order only.

So, can any one guide me the way to implement the technique to use so that the sorting becomes faster.

class Car implements Comparable<Car> {     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<Car> {         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<Car> carList = new ArrayList<>();         List<String> sortOrder = new ArrayList<>();          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                  } } 
like image 437
akhtar Avatar asked Mar 09 '11 11:03

akhtar


People also ask

How do I sort a custom Comparator?

Using a comparator, we can sort the elements based on data members. For instance, it may be on roll no, name, age, or anything else. Method of Collections class for sorting List elements is used to sort the elements of List by the given comparator.

Can we use comparable for custom sorting?

Both Comparable and Comparator can be used for custom sorting but there are some differences in their usage. Comparable interface can be used to provide one way of sorting whereas Comparator interface can be used to provide multiple ways of sorting.

What is a custom Comparator?

Custom Comparator are used to compare the objects of user-defined classes. Syntax: bool comp(object o1, object o2) { // There can be any condition implemented as per the need of the problem statement. // For Example: return (o1.data_member == o2.data_member);

What is customized sorting order?

Custom Sort allows you to create your order in which you want to sort the data. Sometimes you do not prefer the existing order and you want to sort it in a different order, then you can always choose to Custom Sort your data. To do Custom Sorting, you just need to create a Custom List.


1 Answers

I recommend you create an enum for your car colours instead of using Strings and the natural ordering of the enum will be the order in which you declare the constants.

public enum PaintColors {     SILVER, BLUE, MAGENTA, RED } 

and

 static class ColorComparator implements Comparator<CarSort>  {      public int compare(CarSort c1, CarSort c2)      {          return c1.getColor().compareTo(c2.getColor());      }  } 

You change the String to PaintColor and then in main your car list becomes:

carList.add(new CarSort("Ford Figo",PaintColor.SILVER));  ...  Collections.sort(carList, new ColorComparator()); 
like image 64
z7sg Ѫ Avatar answered Sep 20 '22 16:09

z7sg Ѫ