在 Java 8 中以不区分大小写的方式对字符串值进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49821774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 10:27:24  来源:igfitidea点击:

Sorting string value in a case-insensitive manner in Java 8

javajava-8java-stream

提问by Learn Hadoop

How do I sort string values in case-insensitive order in the following?

如何按以下不区分大小写的顺序对字符串值进行排序?

List<Employee> listofEmployees = Arrays.asList(
    new Employee(1, "aaa", Arrays.asList(123, 345, 678)),
    new Employee(1, "bbb", Arrays.asList(91011, 121314, 1516117)),
    new Employee(2, "ccc", Arrays.asList(181920, 212223, 242526)),
    new Employee(3, "ddd", Arrays.asList(272829, 303132, 333435)),
    new Employee(4, "BBB", Arrays.asList(29, 332, 33))
);

I wrote like this:

我是这样写的:

listofEmployees.stream().sorted(Comparator.comparing(Employee::getName).reversed())
        .forEach(s -> System.out.println(s.getName()));

How do I pass a string case insensitive option here?

如何在此处传递字符串不区分大小写的选项?

回答by Hadi J

Try this

试试这个

Comparator.comparing(Employee::getName, String.CASE_INSENSITIVE_ORDER)

回答by Oleksandr Pyrohov

As @Tree suggested in comments, one can use the java.text.Collatorfor a case-insensitive and locale-sensitive Stringcomparison. The following shows how both case and accents could be ignored for US English:

正如@Tree 在评论中建议的那样,可以将java.text.Collator用于不区分大小写和区分区域设置的String比较。下面显示了如何在美国英语中忽略大小写和重音:

Collator collator = Collator.getInstance(Locale.US);
collator.setStrength(Collator.PRIMARY);
listOfEmployees.sort(Comparator.comparing(Employee::getName, collator.reversed()));

When collator strength is set to PRIMARY, then only PRIMARYdifferences are considered significant during comparison. Therefore, the following Strings are considered equivalent:

当 collat​​or strength 设置为 时PRIMARY,则PRIMARY在比较期间仅将差异视为显着。因此,以下字符串被认为是等效的:

if (collator.compare("abc", "ABC") == 0) {
    System.out.println("Strings are equivalent");
}

回答by Schidu Luca

You can specify it as the second argument to ignore cases:

您可以将其指定为忽略大小写的第二个参数:

Comparator.comparing(Employee::getName, String::compareToIgnoreCase).reversed()

回答by shakhawat

This is one of many ways:

这是许多方法之一:

listofEmployees.stream()
    .sorted((o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName()))
    .forEach(s -> System.out.println(s.getName()));

回答by genhernandez

It looks like there is a Comparator that orders String objects as by compareToIgnoreCase, you can read the documentation here: https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#CASE_INSENSITIVE_ORDER

看起来有一个比较器通过 compareToIgnoreCase 对字符串对象进行排序,您可以在这里阅读文档:https: //docs.oracle.com/javase/8/docs/api/java/lang/String.html#CASE_INSENSITIVE_ORDER

回答by Ousmane D.

You can lowercase or uppercase it like this:

您可以像这样小写或大写:

listofEmployees.stream()
               .sorted(Comparator.comparing((Employee e) -> e.getName()
                                 .toLowerCase()) // or toUpperCase
                                 .reversed())
               .forEach(s -> System.out.println(s.getName()));