package com.sagacloud.utils.objects;
/*
 * Author: Jxing
 * Create Time: 2017/9/25
 */

import com.alibaba.fastjson.*;

import java.util.Comparator;

public class ObjectComparator implements Comparator<JSONObject> {
    String key;
    public ObjectComparator(String compareKey) {
        key = compareKey;
    }

    @Override
    public int compare(JSONObject o1, JSONObject o2) {
        if(o1.containsKey(key) && o2.containsKey(key))
        {
            if(o1.get(key) == null)
                return -1;
            if(o2.get(key) == null)
                return  1;
            return o2.get(key).toString().compareTo(o1.get(key).toString());
        }
        return 0;
    }
}
