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

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);
    }
}


我想使用整理器来实现以下排序:

AaÄäbbCcUuüü

像这样的东西

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


如何在排序代码中使用此整理器?

最佳答案

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

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);
        }
    }
});

09-26 14:10