Sorting list of objects based on nested values in java

champ.exe :

I have a list of Cars scheduled for delivery for multiple dates which needs to be sorted on the basis of the below points:

  • If isReady>0, then it should be displayed first in the table. And then the other values come below it for that particular date.
  • If isReady>0 and Object gear!=null then, it is displayed first in the table for that particular date. Followed by the other values where Object gear==null.
  • If isReady>0, Object gear!=null and Object tyre!=null, then that value is displayed first in the table for that particular date. Followed by the other values where Object gear==null and tyre==null.

Here are the class codes:

public class Car {
    private int isReady;
    private Tyre tyre;
    private Gear gear;
    private Date deliveryDate;
}


public class Gear {
    private int id;
    private String type;
}


public class Tyre {
    private int id;
    private String grip;
}

public class CarComparator implements Comparator<Car> {
    @Override
    public int compare(Car entry1, Car entry2) {
        int value = 0;

        if (entry1.getIsReady() > entry2.getIsReady()) {
            value = -1;
        } else if (entry1.getIsReady() < entry2.getIsReady()) {
            value = 1;
        } else if (entry1.getIsReady() == entry2.getIsReady()) {
            value = 0;
        }
        return value;
    }
}

I have developed a Comparator which works fine for the first condition where isReady>0. Could you please help me with the other conditions mentioned above.

Thanks in advance.

Ioannis Barakos :

Check this comparator so you can sort with multiple attributes

public class CarComparator implements Comparator<Car> {

    @Override
    public int compare(Car entry1, Car entry2) {
        int value;
        if (entry1.getDeliveryDate().before(entry2.getDeliveryDate())){
            value = -1;
        }else if (entry1.getDeliveryDate().equals(entry2.getDeliveryDate())){
            value = 0;
        }else{
            value =1;
        }
        //For same day
        if (value==0){
            if (entry1.getIsReady() > entry2.getIsReady()) {
                value = -1;
            } else if (entry1.getIsReady() < entry2.getIsReady()) {
                value = 1;
            } else if (entry1.getIsReady() == entry2.getIsReady()) {
                value = 0;
            }
        }
        //if same isReady
        if (value==0){
            if (entry1.getGear()!=null && entry2.getGear()==null) {
                value = -1;
            } else  if (entry1.getGear()==null && entry2.getGear()==null) {
                value = 0;
            } else{
                value = 1;
            }
        }
        //if still equals
        if (value==0){
            if (entry1.getTyre()!=null && entry2.getTyre()==null) {
                value = -1;
            } else  if (entry1.getTyre()==null && entry2.getTyre()==null) {
                value = 0;
            } else{
                value = 1;
            }
        }


        return value;
    }
}

I am not sure if this is what you try to do. What the above comparator does is: First to sort with Dates, if it finds equal dates (value=0), it compares the isReady, then getGear() and finally the getTyre().

That way you can add as many attributes as you need in your comparator.

Including the main method with 3 cars

public class Main {
    public static void main (String[]args) throws UnsupportedEncodingException, ParseException {

        List<Car> carL = new ArrayList<Car>();

        Car car1 = new Car();
        car1.setDeliveryDate(new Date());
        Gear gear1 = new Gear();
        car1.setGear(gear1);
        Tyre tyre1 = new Tyre();
        car1.setTyre(null);
        car1.setId(1);
        car1.setDeliveryDate((new SimpleDateFormat("dd-MM-yyyy")).parse("01-01-2000"));
        car1.setIsReady(0);

        Car car2 = new Car();
        car2.setDeliveryDate(new Date());
        Gear gear2 = new Gear();
        car2.setGear(gear2);
        Tyre tyre2 = new Tyre();
        car2.setTyre(tyre2);
        car2.setId(2);
        car2.setDeliveryDate((new SimpleDateFormat("dd-MM-yyyy")).parse("02-01-2000"));

        car2.setIsReady(1);

        Car car3 = new Car();
        car3.setDeliveryDate(new Date());
        Gear gear3 = new Gear();
        car3.setGear(gear3);
        Tyre tyre3 = new Tyre();
        car3.setTyre(tyre3);
        car3.setId(3);
        car3.setDeliveryDate((new SimpleDateFormat("dd-MM-yyyy")).parse("01-01-2000"));

        car3.setIsReady(1);

        carL.add(car1);
        carL.add(car2);
        carL.add(car3);
        Collections.sort(carL, new CarComparator());
        for (Car car : carL) {
            System.out.println("car: " + car.toString());
        }
    }
}

outputs:

car: Car{id=3, isReady=1, tyre=false, gear=false, deliveryDate=Sat Jan 01 00:00:00 EET 2000}
car: Car{id=1, isReady=0, tyre=true, gear=false, deliveryDate=Sat Jan 01 00:00:00 EET 2000}
car: Car{id=2, isReady=1, tyre=false, gear=false, deliveryDate=Sun Jan 02 00:00:00 EET 2000}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=306355&siteId=1