如何在java中为每个属性实现多个排序顺序的多属性排序

时间:2018-01-11 13:35:46

标签: java sorting

我正在尝试为对象列表的每个属性实现sql排序。有点像这样 - 但是用java

    SELECT Id, CompanyName, City, Country FROM Supplier WHERE Country IN ('USA', 'Japan', 'Germany') ORDER BY Country ASC, CompanyName DESC

我已经查看/使用了像ComparisonChain,lambdaj这样的库来实现这种行为。但两者都缺乏添加多个排序顺序的功能,如SQL。 将国家排序为升序,将名称排序为降序。 我甚至试图在自己的代码中编写代码;基于排序值我发送返回值的-ve但由于某些原因没有给出所需的结果。

public int compare(Item a, Item b)
{

    int compareOwner = a.getOwner().compareTo(b.getOwner());
    int compareCreated = a.getCreationTime().compareTo(b.getCreationTime());
    Map<String, Integer> compareMap = new HashMap<String, Integer>();
    compareMap.put("user", compareOwner);
    compareMap.put("creationTime", compareCreated);

    Map<String, Object> map = a.getProperties();

    for (Map.Entry<String, Object> ent : map.entrySet())
    {
        if (b.getCustomProperties().containsKey(ent.getKey()))
        {
            Object a1 = ent.getValue();
            Object b1 = b.Properties().get(ent.getKey());
            if (a1 instanceof String && b1 instanceof String)
            {
                String val = (String) ent.getValue();
                int cmp = val.compareTo((String) b.getCustomProperties().get(ent.getKey()));
                compareMap.put(ent.getKey(), cmp);
            }
            else if (a1 instanceof Integer && b1 instanceof Integer)
            {
                Integer val = (Integer) ent.getValue();
                int cmp = val.compareTo((Integer) b.getCustomProperties().get(ent.getKey()));
                compareMap.put(ent.getKey(), cmp);
            }
            else if (a1 instanceof Float && b1 instanceof Float)
            {
                Float val = (Float) ent.getValue();
                int cmp = val.compareTo((Float) b.getCustomProperties().get(ent.getKey()));
                compareMap.put(ent.getKey(), cmp);
            }
            else if (a1 instanceof Double && b1 instanceof Double)
            {
                Double val = (Double) ent.getValue();
                int cmp = val.compareTo((Double) b.getCustomProperties().get(ent.getKey()));
                compareMap.put(ent.getKey(), cmp);
            }
            else if (a1 instanceof Date && b1 instanceof Date)
            {
                Date val = (Date) ent.getValue();
                int cmp = val.compareTo((Date) b.getCustomProperties().get(ent.getKey()));
                compareMap.put(ent.getKey(), cmp);
            }
        }

    }
    int returnValue = 0;
    for (Map.Entry<String, Boolean> entry : fields.entrySet())
    {
        if (compareMap.get(entry.getKey()) != null && compareMap.get(entry.getKey()) == 0)
        {
            continue;
        }
        else if (compareMap.get(entry.getKey()) != null)
        {

            if (entry.getValue() == false)
            {
                return returnValue = -returnValue;
            }
            return returnValue = compareMap.get(entry.getKey());
        }
        else
        {
            continue;
        }

    }

    return returnValue;
}

其中fields变量是属性及其相应顺序的映射。

是否有任何替代库或改进此比较器以支持多重排序。

0 个答案:

没有答案