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

后端 未结 9 1143
生来不讨喜
生来不讨喜 2020-11-22 08:40

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 alphabeti

9条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 09:07

    Define one Enum Type as

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

    Change data type of color from String to Colors Change return type and argument type of getter and setter method of color to Colors

    Define comparator type as follows

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

    after adding elements to List, call sort method of Collection by passing list and comparator objects as arguments

    i.e, Collections.sort(carList, new ColorComparator()); then print using ListIterator.

    full class implementation is as follows:

    package test;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;    
    import java.util.ListIterator;
    
    public class CarSort implements Comparable{
    
        String name;
        Colors color;
    
        public CarSort(String name, Colors color){
            this.name = name;
            this.color = color;
        } 
    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Colors getColor() {
            return color;
        }
        public void setColor(Colors color) {
            this.color = color;
        }
    
        //Implement the natural order for this class
        public int compareTo(CarSort c)
        {
            return getName().compareTo(c.getName());
        }
    
        static class ColorComparator implements Comparator
        {
            public int compare(CarSort c1, CarSort c2)
            {
                return c1.getColor().compareTo(c2.getColor());
            }
        }
    
        public enum Colors {
             BLUE, SILVER, MAGENTA, RED
        }
    
         public static void main(String[] args)
         {
             List carList = new ArrayList();
             List sortOrder = new ArrayList();
    
             carList.add(new CarSort("Ford Figo",Colors.SILVER));
             carList.add(new CarSort("Santro",Colors.BLUE));
             carList.add(new CarSort("Honda Jazz",Colors.MAGENTA));
             carList.add(new CarSort("Indigo V2",Colors.RED));
             Collections.sort(carList, new ColorComparator());
    
             ListIterator itr=carList.listIterator();
             while (itr.hasNext()) {
                CarSort carSort = (CarSort) itr.next();
                System.out.println("Car colors: "+carSort.getColor());
            }
         }
    }
    

提交回复
热议问题