对集合中的map排序

根据集合中的map某个字段(orderBy参数),这个字段可以是数字型,字符型,日期型,也可以是map型,来排序,可以升序也可以降序,通过orderType参数("asc"升序,"desc"降序)。这个是从com.alibaba.druid.util提取出来的。

源码如下:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.alibaba.druid.util;

import com.alibaba.druid.util.StringUtils;
import java.lang.reflect.Array;
import java.text.Collator;
import java.util.Comparator;
import java.util.Date;
import java.util.Map;

public class MapComparator<K, V> implements Comparator<Map<K, V>> {
    private boolean isDesc;
    private K orderByKey;

    public MapComparator(K orderByKey, boolean isDesc) {
        this.orderByKey = orderByKey;
        this.isDesc = isDesc;
    }

    private int compare(Number o1, Number o2) {
        return (int)(o1.doubleValue() - o2.doubleValue());
    }

    private int compare(String o1, String o2) {
        return Collator.getInstance().compare(o1, o2);
    }

    private int compare(Date o1, Date o2) {
        return (int)(o1.getTime() - o2.getTime());
    }

    public int compare(Map<K, V> o1, Map<K, V> o2) {
        int result = this.compare_0(o1, o2);
        if(this.isDesc) {
            result = -result;
        }

        return result;
    }

    private Object getValueByKey(Map<K, V> map, K key) {
        if(key instanceof String) {
            String keyStr = (String)key;
            if(keyStr.matches(".+\\[[0-9]+\\]")) {
                Object value = map.get(keyStr.substring(0, keyStr.indexOf(91)));
                if(value == null) {
                    return null;
                }

                Integer index = StringUtils.subStringToInteger(keyStr, "[", "]");
                if(value.getClass().isArray() && Array.getLength(value) >= index.intValue()) {
                    return Array.get(value, index.intValue());
                }

                return null;
            }
        }

        return map.get(key);
    }

    private int compare_0(Map<K, V> o1, Map<K, V> o2) {
        Object v1 = this.getValueByKey(o1, this.orderByKey);
        Object v2 = this.getValueByKey(o2, this.orderByKey);
        return v1 == null && v2 == null?0:(v1 == null?-1:(v2 == null?1:(v1 instanceof Number?this.compare((Number)v1, (Number)v2):(v1 instanceof String?this.compare((String)v1, (String)v2):(v1 instanceof Date?this.compare((Date)v1, (Date)v2):0)))));
    }
}
调用方法:

 public static void main(String args[]){
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> map2 = new HashMap<String, Object>();
        map.put("amount2","1.2");
        map2.put("amount2","1.3");
        list.add(map);
        list.add(map2);
//        "asc"升序,"desc"降序
        String orderType = "asc";
        String orderBy = "amount2";
        Collections.sort(list,new MapComparator(orderBy, "desc".equals(orderType)));

        System.out.println(list);

    }

结果:
[{amount2=1.2}, {amount2=1.3}]

猜你喜欢

转载自blog.csdn.net/Wewon_real/article/details/79009557