我正在尝试按具有特定区域设置的字段对列表进行排序。我如何结合以下陈述?
names.stream().sorted(Comparator.comparing(AClass::getName)).collect(Collectors.toList());
Collator coll = Collator.getInstance(new Locale("tr","TR"));
coll.setStrength(Collator.PRIMARY);
答案 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());