java Collections 排序--多条件排序

java Collections 排序–多条件排序

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

// 告警排序
Comparator comparator = new Comparator() {
@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;
    }

你可能感兴趣的:(技术文档)