比较器(sort)以按优先级比较具有3个属性的对象的数组列表

时间:2015-12-08 19:36:22

标签: java sorting comparator jcr

因此,假设有一个节点,它有三个属性:(85653965,) pdf:Title& dc:title,我想基于这些属性对数组进行比较和排序,优先级相同。因此,如果node1具有所有三个属性且node2仅具有node name,则它应将node1的dc:title与node2的pdf:Title进行比较。

这是我正在使用的代码:

dc:title

问题:它没有正确排序,它们不是按字母顺序排列的。我错过了什么吗?

还有一种更好的方法可以在不使用这么多条件的情况下编写代码吗?我试图避开@Override public int compare(Node o1, Node o2) { try { int compareValue; boolean o1HasPdfTitle = o1.hasProperty("jcr:content/metadata/pdf:Title"); boolean o1HasDcTitle = o1.hasProperty("jcr:content/metadata/dc:title"); boolean o2HasPdfTitle = o2.hasProperty("jcr:content/metadata/pdf:Title"); boolean o2HasDcTitle = o2.hasProperty("jcr:content/metadata/dc:title"); if (o1HasPdfTitle && o2HasPdfTitle) { compareValue = o1.getProperty("jcr:content/metadata/pdf:Title").getString().compareTo(o2.getProperty("jcr:content/metadata/pdf:Title").getString()); return compareValue; } else if (o1HasPdfTitle && o2HasDcTitle) { compareValue = o1.getProperty("jcr:content/metadata/pdf:Title").getString().compareTo(o2.getProperty("jcr:content/metadata/dc:title").getString()); return compareValue; } else if (o1HasDcTitle && o2HasPdfTitle) { compareValue = o1.getProperty("jcr:content/metadata/dc:title").getString().compareTo(o2.getProperty("jcr:content/metadata/pdf:Title").getString()); return compareValue; } else if (o1HasDcTitle && o2HasDcTitle) { compareValue = o1.getProperty("jcr:content/metadata/dc:title").getString().compareTo(o2.getProperty("jcr:content/metadata/dc:title").getString()); return compareValue; } else if (!o1HasPdfTitle && !o1HasDcTitle && o2HasPdfTitle){ compareValue = o1.getName().compareTo(o2.getProperty("jcr:content/metadata/pdf:Title").getString()); return compareValue; } else if (!o1HasPdfTitle && !o1HasDcTitle && o2HasDcTitle){ compareValue = o1.getName().compareTo(o2.getProperty("jcr:content/metadata/dc:title").getString()); return compareValue; } else if (o1HasPdfTitle && !o2HasPdfTitle && !o2HasDcTitle){ compareValue = o1.getProperty("jcr:content/metadata/pdf:Title").getString().compareTo(o2.getName()); return compareValue; } else if (o1HasDcTitle && !o2HasPdfTitle && !o2HasDcTitle){ compareValue = o1.getProperty("jcr:content/metadata/dc:title").getString().compareTo(o2.getName()); return compareValue; } else { compareValue = o1.getName().compareTo(o2.getName()); return compareValue; } } catch (Exception e) { LOGGER.debug("CustomComparatorTitle debug message" + e); return 0; }

1 个答案:

答案 0 :(得分:1)

我不确定我是否理解您的代码,但它似乎以这种方式更好:

    final String pPdf="jcr:content/metadata/pdf:Title";
    final String pDC="jcr:content/metadata/dc:title";

    String[]  values=new String[2];
    for (int i = 0; i < values.length; i++)
    {
        Node curNode=i==0?o1:o2;

        if(curNode.hasProperty(pPdf))
            values[i]=curNode.getProperty(pPdf).getString();
        else if(curNode.hasProperty(pDC))
            values[i]=curNode.getProperty(pDC).getString();
        else
            values[i]=curNode.getName();
    }
    return values[0].compareTo(values[1]);

它不是更好吗?