0

我正在尝试使用以下代码对节点列表进行排序:

private String sortPropertyName;
private void sortListUsingSortorder(List<MyNode> nodes, String sortOrder) {
        BeanComparator comparator = new BeanComparator(sortPropertyName, new Comparator() {
        public int compare(Object obj1, Object obj2) {
            if ("dsc".equals(sortOrder)) {
                return ((Comparable) obj2).compareTo(obj1);
            } else {
                return ((Comparable) obj1).compareTo(obj2);
            }
            }
        });

    Collections.sort(nodes, comparator);
    }
}

我想使用 Collat​​or 来实现以下排序:

AaÄäBbCcUuÜü

像这样的东西

    Collator collator = Collator.getInstance(Locale.GERMAN);
    collator.setStrength(Collator.PRIMARY);
    Collections.sort(words, collator);

如何在我的排序代码中使用这个 Collat​​or?

4

1 回答 1

1

委托给比较器中的整理者:

BeanComparator comparator = new BeanComparator(sortPropertyName, new Comparator() {
    public int compare(Object obj1, Object obj2) {
        if ("dsc".equals(sortOrder)) {
            return collator.compare(obj2, obj1);
        } 
        else {
            return collator.compare(obj1, obj2);
        }
    }
});
于 2012-11-23T10:36:51.447 回答