简体   繁体   English

java-使用集合对员工姓名列表进行排序

[英]java - sorting the list of employee names using collections

I think this may be a duplicate question. 我认为这可能是一个重复的问题。 But couldn't find the answer for my requirement. 但是找不到我的要求的答案。

I have a list of names (String) such as 我有一个名称列表(字符串),例如

Merill, Gopi, kamal, orange, white

I need to do something to get the list in ascending order using collections like the following 我需要做一些事情,使用如下所示的集合按升序获取列表

Gopi, kamal, Merill, orange, white

Is it possible to get the list in alphabeticalorder? 是否可以按字母顺序获取列表?

Can anyone please tell how to sort this using collections? 谁能告诉我如何使用集合对它进行排序?

(Please provide a solution before closing this questiona as duplicate) (请在关闭此问题之前,提供解决方案)

You can use this: 您可以使用此:

Collections.sort(list, String.CASE_INSENSITIVE_ORDER)

String.CASE_INSENSITIVE_ORDER is a Comparator<String> in String class. String.CASE_INSENSITIVE_ORDERString类中的Comparator<String> Note that this is not locale-aware. 请注意,这不支持区域设置。

You've already mentioned the solution in your post: 您已经在帖子中提到了解决方案:

Can anyone please tell how to sort this using collectins? 谁能告诉我如何使用collectins对其进行排序?

Use java.util.Collections.sort(...) . 使用java.util.Collections.sort(...)

The API for Collections . 集合的API

Collections.sort(pass the list) will soft in ascending order. Collections.sort(通过列表)将按升序顺序变软。 If you want to sort your own way then you need to use Comparator but here there is no need of doing that. 如果您想以自己的方式进行排序,则需要使用Comparator,但是这里不需要这样做。

I know your trying to sort it using Collections, but here's a simple way: 我知道您尝试使用“收藏夹”对其进行排序,但这是一种简单的方法:

public static void main(String[] args) throws ParseException {
    String[] values = new String[] {"Some item", "another item", "1 last more", "nevermind", "this is different"};
    System.out.println("Before: " + Arrays.toString(values));
    Arrays.sort(values, new Comparator<String>() {
        @Override
        public int compare(String arg0, String arg1) {
            return arg0.compareTo(arg1);
        }
    });
    System.out.println("After: " + Arrays.toString(values));
}

And this is outputted: 并输出:

Before: [Some item, another item, 1 last more, nevermind, this is different] 之前:[某些项目,另一项目,最后1个,没关系,这是不同的]

After: [1 last more, Some item, another item, nevermind, this is different] 之后:[还有1个,另外一个项目,另一个项目,没关系,这是不同的]

The array is sorted numerical/other first, then capital-alphabetical, then lowercase-alphabetical 数组按数字/其他顺序排序,然后按大写字母排序,然后按小写字母排序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM