我有一个计划在多个日期交付的汽车清单, 需要根据以下几点进行排序:
isReady>0,
,则应首先在表中显示它。和
那么该特定日期的其他值就会低于该值。isReady>0
和对象gear!=null
,则它首先显示在
该特定日期的表格。其次是对象gear==null
所在的其他值。 isReady>0
,对象gear!=null
和对象tyre!=null
,则表示
该特定日期的值首先显示在表格中。其次是对象gear==null
和tyre==null
的其他值。以下是class
代码:
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;
}
}
我开发了一种比较器,可以在第一个条件下正常工作
isReady>0
。你能帮我解决其他情况吗 上面提到过。
谢谢。
答案 0 :(得分:2)
检查此比较器,以便可以对多个属性进行排序
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;
}
}
我不确定这是否是您要尝试的方法。以上比较器的作用是: 首先使用日期排序,如果找到相等的日期(值= 0),则比较isReady,然后比较getGear()和最后的getTyre()。
这样,您可以在比较器中添加所需数量的属性。
包括3辆车的主要方法
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());
}
}
}
输出:
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}
答案 1 :(得分:2)
好吧,从Java 8开始,您可以像这样构建比较器:
//order by delivery date first, ascending order
Comparator<Car> carComparator = Comparator.comparing( Car::getDeliveryDate )
//order by isReady in ascending order
.thenComparing( Car::getIsReady )
//we map null to 1 and non-null to -1 and ignore the rest for now
.thenComparing( car -> car.getGear() != null ? -1 : 1 )
.thenComparing( car -> car.getTyre() != null ? -1 : 1 );
答案 2 :(得分:0)
说实话,您的代码没有发现任何问题。也就是说,如果您的意图是在entry1大于entry 2(规范的相反顺序)时返回-1。没有更多的代码可供阅读,我认为如果您尝试比较为您的用例构建的更大或更小的值,您的代码将可以正常工作。
但是,我认为您的退货方法效率低下。您不需要返回值。您可以返回一个实际值。
为进行比较,您可以先比较==,然后再评估其余部分。但这可能很难阅读代码,所以我给你两个版本。
删除值版本:
public class CarComparator implements Comparator<Car> {
@Override
public int compare(Car entry1, Car entry2) {
if (entry1.getisReady() > entry2.getisReady()) {
return -1;
} else if (entry1.getisReady() < entry2.getisReady()) {
return 1;
} else if (entry1.getisReady() == entry2.getisReady()) {
return 0;
}
}
}
去除价值和不同的比较方式:
public class CarComparator implements Comparator<Car> {
@Override
public int compare(Car entry1, Car entry2) {
if (entry1.getisReady() == entry2.getisReady()) return 0;
return entry1.getisReady() > entry2.getisReady()? -1 : 1;
}
}
我不确定我是否正确理解了您,但是如果我没有正确理解,希望此代码可以为您提供帮助。我将它们分为3种方法,希望您得到一个getTyre()和getGear()方法。您可以根据需要将它们组合起来,对于最后一种方法,其值被分成可变的顺序以便于阅读代码。
class CarComparator implements Comparator<Car> {
public int compare(Car entry1, Car entry2) {
if (entry1.getisReady() == entry2.getisReady()) return 0;
return entry1.getisReady() > entry2.getisReady()? -1 : 1;
}
public int compareGear(Car entry1, Car entry2){
if ( (entry1.getGear() != null && entry2.getGear() != null)
||(entry1.getGear() == null && entry2.getGear() == null)
){
return compare(entry1, entry2);
}
return entry1.getGear() != null && entry2.getGear() == null? -1 : 1;
}
public int compareTye(Car entry1, Car entry2){
int order1 = entry1.getGear() != null && entry1.getTyre() != null? 1 : 0;
int order2 = entry2.getGear() != null && entry2.getTyre() != null? 1 : 0;
if ( order1 == order2 ) return compare(entry1, entry2);
return order1 > order2? -1 : 1;
}
}
答案 3 :(得分:0)
为什么不重用Integer.compareTo来使代码更短?
像这样:
import java.util.Comparator;
public class CarComparator implements Comparator<Car> {
@Override
public int compare(Car entry1, Car entry2) {
int value = 0;
// might want to add a null check for either entry1 and entry2
value = entry1.getDeliveryDate().compareTo(entry2.getDeliveryDate());
if (value == 0) {
value = ((Integer)entry1.getIsReady()).compareTo((Integer)entry2.getIsReady());
if (value == 0) {
value = getIntegerValueForNullCheck(entry1.getGear()).compareTo(getIntegerValueForNullCheck(entry2.getGear()));
if (value == 0) {
value = getIntegerValueForNullCheck(entry1.getTyre()).compareTo(getIntegerValueForNullCheck(entry2.getTyre()));
}
}
}
return value;
}
private Integer getIntegerValueForNullCheck (Object o) {
return o == null ? 0 : 1;
}
}
包括测试排序的代码:
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.*;
public class Sorting {
public static void main(String[] args) {
List<Car> cars = new LinkedList<>();
Date today = new Date();
Instant now = Instant.now();
Instant after = now.plus(Duration.ofDays(1));
Date tomorrow = Date.from(after);
cars.add(new Car(5, new Tyre(1,"1"), new Gear(1, "1"), today ));
cars.add(new Car(5, new Tyre(1,"1"), null, today ));
cars.add(new Car(5, null, null, today ));
cars.add(new Car(4, null, null, today ));
cars.add(new Car(3, null, null, tomorrow ));
Collections.sort(cars, new CarComparator());
System.out.println(cars);
}
}
输出:
[Car{isReady=4, tyre=null, gear=null, deliveryDate=Thu Oct 10 11:27:20 IDT 2019}
, Car{isReady=5, tyre=null, gear=null, deliveryDate=Thu Oct 10 11:27:20 IDT 2019}
, Car{isReady=5, tyre=Tyre{id=1, grip='1'}, gear=null, deliveryDate=Thu Oct 10 11:27:20 IDT 2019}
, Car{isReady=5, tyre=Tyre{id=1, grip='1'}, gear=Gear{id=1, type='1'}, deliveryDate=Thu Oct 10 11:27:20 IDT 2019}
, Car{isReady=3, tyre=null, gear=null, deliveryDate=Fri Oct 11 11:27:20 IDT 2019}
]