Java如何使用locale对字段进行排序?

时间:2018-02-19 12:53:00

标签: java lambda java-8

我正在尝试按具有特定区域设置的字段对列表进行排序。我如何结合以下陈述?

names.stream().sorted(Comparator.comparing(AClass::getName)).collect(Collectors.toList());

Collator coll = Collator.getInstance(new Locale("tr","TR"));
coll.setStrength(Collator.PRIMARY);

2 个答案:

答案 0 :(得分:5)

Comparator.comparing的变体带有两个参数。第二个参数是比较器,因此您可以将Collator实例作为第二个参数传递:

Collator coll = Collator.getInstance(new Locale("tr","TR"));
coll.setStrength(Collator.PRIMARY);
List<AClass> sorted = names.stream()
                           .sorted(Comparator.comparing(AClass::getName,coll))
                           .collect(Collectors.toList());

答案 1 :(得分:0)

    List<Model> sortedModelList = unSortedModelList.stream()
    .sorted(Comparator.comparing(Model::getField,Collator.getInstance(new Locale("tr","TR"))))
.collect(Collectors.toList());