List<Map<String, Object>>根据字段排序

/**
 * List>根据字段排序
 * @param list 数据
 * @param feild 排序字段
 * @param type 排序方式 asc/desc
 * @return 排序结果
 */
public List<Map<String, Object>> sortMap(List<Map<String, Object>> list, String feild, String type) {
   if (CollectionUtils.isNotEmpty(list)) {
       list.sort((m1, m2) -> {
           if (StringUtils.equals(type, "desc")) {
               return String.valueOf(m2.get(feild)).compareTo(String.valueOf(m1.get(feild)));
           } else {
               return String.valueOf(m1.get(feild)).compareTo(String.valueOf(m2.get(feild)));
           }
       });
       // Lambda排序
       // Collections.sort(list, (m1, m2)-> String.valueOf(m1.get(feild)).compareTo(String.valueOf(m2.get(feild))));
   }

   return list;
}

你可能感兴趣的:(java)