方法一:
List<String> monthMap = new ArrayList<>(); //月份
monthMap.add("6");
monthMap.add("5");
monthMap.add("4");
monthMap.add("3");
System.out.println("正序"+monthMap);
Collections.reverse(monthMap); //使用方法进行逆序
System.out.println("倒序:"+monthMap);
输出结果:
正序[6, 5, 4]
倒序:[4, 5, 6]
方法二:
List<Map<String, Object>> textList = new LinkedList();
Map<String, Object> map = new LinkedHashMap<>();
String name= String.format("%.4f", (double)0.41124);
String str = String.format("%.4f", (double)0.11241);
map.put("name",name);
map.put("num",str);
// 自动造数据吧。。。。。。。。
textList.add(map)
// 调用方法进行排序
List<Map<String, Object>> mapList =sortToMap(textList,"num","asc");
/**
* map 排序共用方法
* @param mapList map列表
* @param sort 排序字段
* @param order 升序:asc 降序 desc
* @return
*/
public static List<Map<String, Object>> sortToMap(List<Map<String, Object>> mapList, final String sort, final String order) {
Collections.sort(mapList, new Comparator<Map<String, Object>>() {
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
if (o1.get(sort) instanceof String) {
String map1value = object2String(o1.get(sort));
String map2value = object2String(o2.get(sort));
if ("DESC".equals(order.toUpperCase())) {
return map1value.compareTo(map2value);
} else {
return map2value.compareTo(map1value);
}
} else {
Double map1value = object2Double(o1.get(sort));
Double map2value = object2Double(o2.get(sort));
if ("DESC".equals(order.toUpperCase())) {
return map1value - map2value > 0 ? 1 : 1;
} else {
return map1value - map2value > 0 ? -1 : 1;
}
}
}
});
return mapList;
}
/**
*将Object转换为Double
*/
public static Double object2Double(Object o){
if(o instanceof BigDecimal){
return ((BigDecimal) o).doubleValue();
}else if(o instanceof String){
return Double.valueOf((String)o);
}else if(o instanceof Integer){
return Double.valueOf((Integer)o);
}else if(o instanceof Double){
return (Double)o;
}else if(o instanceof Long){
return ((Long) o).doubleValue();
}else{
return 0.0;
}
}
/**
* 对象类型转换为字符串
*
* @param obj
* 参数
* @return String 字符串
*/
public static String object2String(Object obj) {
return convertNull2String(String.valueOf(obj));
}
/**
* 将null或null字符串转化为空字符串,并trim
*
* @param str
* @return
*/
public static String convertNull2String(String str) {
String res = null;
if (str == null || "null".equals(StringUtils.trim(str))) {
res = "";
} else {
res = StringUtils.trim(str);
}
return res;
}