您的位置:首页 > 编程语言 > Java开发

java Collections 排序--多条件排序

2016-07-01 16:52 501 查看

java Collections 排序–多条件排序

// 告警排序
Collections.sort(domesticAirport, comparator);

// 告警排序
Comparator<AirportRtWeatherWarningBeanForTable> comparator = new Comparator<AirportRtWeatherWarningBeanForTable>() {
@Override
public int compare(AirportRtWeatherWarningBeanForTable a1, AirportRtWeatherWarningBeanForTable a2) {
// 首先根据是否过期排序
if(a1.isMetarExpired() && !a2.isMetarExpired()){
return 1;
}else if (!a1.isMetarExpired() && a2.isMetarExpired()) {
return -1;
} else if ((a1.isMetarExpired() && a2.isMetarExpired()) || (!a1.isMetarExpired() && !a2.isMetarExpired())) {
//同时过期,或者都不过期,则按告警类型排序
if (a1.getWarningColor() != a2.getWarningColor()) {
return compareColor(a1.getWarningColor(), a2.getWarningColor());
}
}
//告警类型相同,按字母排序
return a1.getCode4().compareTo(a2.getCode4());
}
};

// 告警类型比较排序
public int compareColor(Color color1, Color color2) {
int i = 0;
int j = 0;

if (color1.equals(MeteoWeatherWarningPanel.RED)) {
i = 3;
} else if (color1.equals(MeteoWeatherWarningPanel.YELLOW)) {
i = 2;
} else if (color1.equals(MeteoWeatherWarningPanel.GREEN)) {
i = 1;
}

if (color2.equals(MeteoWeatherWarningPanel.RED)) {
j = 3;
} else if (color2.equals(MeteoWeatherWarningPanel.YELLOW)) {
j = 2;
} else if (color2.equals(MeteoWeatherWarningPanel.GREEN)) {
j = 1;
}
int k = i - j;
if (k > 0) {
return -1;
} else if (k < 0) {
return 1;
} else
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息