Я создал пример класса, надеюсь, он поможет.
package com;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @author jinleizhang
*/
public class ComparatorTest {
static class Car {
String name;
boolean up;
String type;
Car(String name, boolean up, String type) {
this.name = name;
this.up = up;
this.type = type;
}
public String getName() {
return this.name;
}
public boolean isUp() {
return this.up;
}
public String getType() {
return this.type;
}
@Override
public String toString() {
return "Car{" +
"name='" + name + '\'' +
", up=" + up +
", type='" + type + '\'' +
'}';
}
}
public static void main(String[] args) {
Car carA = new Car("NewCar", false, "type2020");
Car carB = new Car("NewCar", true, "type2019");
Car carC = new Car("OldCar", true, "type1990");
List<Car> cars = new ArrayList<>();
cars.add(carA);
cars.add(carB);
cars.add(carC);
Collections.sort(cars, (x, y) -> {
if (x.getName().equals(y.getName())) {
return x.isUp() ? -1 : 1;
}
return x.getType().compareTo(y.getType());
});
for (var car : cars) {
System.out.println(car);
}
}
}
вывод
Car{name='OldCar', up=true, type='type1990'}
Car{name='NewCar', up=true, type='type2019'}
Car{name='NewCar', up=false, type='type2020'}