首页 > 编程语言 > java Collections 排序--多条件排序实例
2020
10-10

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;
  }

补充知识:Collections.sort多字段排序-指定排序方式-指定排序字段

看代码吧~

1、创建需要排序的字段数组

String [] sortNameArr = {“one”, “tow”,“startDate”};

2、为每个字段执行排序规则

boolean[] isAs = {false, false, false};

3、重写sort方法进行排序

ListUtils.sort(list, sortNameArr, isAs);

public static <E> void sort(List<E> list, final String[] sortnameArr, final boolean[] typeArr) {
    if (sortnameArr.length != typeArr.length) {
      throw new RuntimeException("属性数组元素个数和升降序数组元素个数不相等");
    }
    Collections.sort(list, new Comparator<E>() {
      public int compare(E a, E b) {
        int ret = 0;
        try {
          for (int i = 0; i < sortnameArr.length; i++) {
            ret = ListUtils.compareObject(sortnameArr[i], typeArr[i], a, b);
            if (0 != ret) {
              break;
            }
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
        return ret;
      }
    });
  }

  private static <E> int compareObject(final String sortname, final boolean isAsc, E a, E b) throws Exception {
    int ret;
    Object value1 = ListUtils.forceGetFieldValue(a, sortname);
    Object value2 = ListUtils.forceGetFieldValue(b, sortname);
    // 两个字段都不为空进行排序
    if (null != value1 && null != value2) {
      String str1 = value1.toString();
      String str2 = value2.toString();
      if (value1 instanceof Number && value2 instanceof Number) {
        int maxlen = Math.max(str1.length(), str2.length());
        str1 = ListUtils.addZero2Str((Number) value1, maxlen);
        str2 = ListUtils.addZero2Str((Number) value2, maxlen);
      } else if (value1 instanceof Date && value2 instanceof Date) {
      // 日期排序
        long time1 = ((Date) value1).getTime();
        long time2 = ((Date) value2).getTime();
        int maxlen = Long.toString(Math.max(time1, time2)).length();
        str1 = ListUtils.addZero2Str(time1, maxlen);
        str2 = ListUtils.addZero2Str(time2, maxlen);
      }
      if (isAsc) {
        ret = str1.compareTo(str2);
      } else {
        ret = str2.compareTo(str1);
      }
      return ret;
    } else if (null == value1 && null != value2) {
    // 比值为空 被比值大
      ret = 1;
    } else {
    // 被比值为空比值打
      ret = -1;
    }
    return ret;
  }

  public static String addZero2Str(Number numObj, int length) {
    NumberFormat nf = NumberFormat.getInstance();
    // 设置是否使用分组
    nf.setGroupingUsed(false);
    // 设置最大整数位数
    nf.setMaximumIntegerDigits(length);
    // 设置最小整数位数
    nf.setMinimumIntegerDigits(length);
    return nf.format(numObj);

  }

  public static Object forceGetFieldValue(Object obj, String fieldName) throws Exception {
    Field field = obj.getClass().getDeclaredField(fieldName);
    Object object = null;
    boolean accessible = field.isAccessible();
    if (!accessible) {
      // 如果是private,protected修饰的属性,需要修改为可以访问的
      field.setAccessible(true);
      object = field.get(obj);
      // 还原private,protected属性的访问性质
      field.setAccessible(accessible);
      return object;
    }
    object = field.get(obj);
    return object;
  }

以上这篇java Collections 排序--多条件排序实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持自学编程网。

编程技巧