简体   繁体   English

java字符串数组排序

[英]java string array sort

I have to sort list of arrays by date. 我必须按日期对数组列表进行排序。 I did not known how to do it, so i have added all values into one value. 我不知道该怎么做,所以我必须添加的所有值到一个值。 This is almost work fine, but it do not sort years good This is my code: 这几乎可以正常工作,但它的年限却不好。这是我的代码:

ArrayList<String> ar = new ArrayList<String>();
        String s1 ="2016|07|21 15:26:20!947!-0.0091002";
        String s2 ="2015|08|21 15:26:20!977!0.0091002";
        String s3 ="2015|07|22 15:26:20!947!1.0091002";
        String s4 ="2015|07|21 16:26:20!946!0.0091001";
        String s5 ="2015|07|21 15:27:20!977!0.0091002";
        String s6 ="2015|07|21 15:26:21!947!1.0091002";
        String s7 ="2015|07|21 15:26:20!946!0.0091001";
        ar.add(s1);
        ar.add(s2);
        ar.add(s3);
        ar.add(s4);
        ar.add(s5);
        ar.add(s6);
        ar.add(s7);
        Collections.sort(ar.subList(1, ar.size()));
        Object[][] data = {
                {ar.get(0)},
                {ar.get(1)},
                {ar.get(2)},
                {ar.get(3)},
                {ar.get(4)},
                {ar.get(5)},
                {ar.get(6)},
        };

and this is output: 这是输出:

2016|07|21 15:26:20!947!-0.0091002
2015|07|21 15:26:20!946!0.0091001
2015|07|21 15:26:21!947!1.0091002
2015|07|21 15:27:20!977!0.0091002
2015|07|21 16:26:20!946!0.0091001
2015|07|22 15:26:20!947!1.0091002
2015|08|21 15:26:20!977!0.0091002

but this is what I need: 但这是我需要的:

2015|07|21 15:26:20!946!0.0091001
2015|07|21 15:26:21!947!1.0091002
2015|07|21 15:27:20!977!0.0091002
2015|07|21 16:26:20!946!0.0091001
2015|07|22 15:26:20!947!1.0091002
2015|08|21 15:26:20!977!0.0091002
2016|07|21 15:26:20!947!-0.0091002

And i've got no ida why is this happend 而且我没有ida,为什么会这样

Simply use: 只需使用:

Collections.sort(ar);

to sort the entire list. 对整个列表进行排序。

Remember that Java lists are zero-based, so ar.get(0) is the first element. 请记住,Java列表是从零开始的,因此ar.get(0)是第一个元素。 List.subList(i, j) returns the sublist from i (inclusively) to j (exclusively). List.subList(i, j)将子列表从i (包括)返回到j (包括)。

So, to sort the whole list via a sublist, you'd need to use i = 0 and j = ar.size() . 因此,要通过子列表对整个列表进行排序,您需要使用i = 0j = ar.size() However, the indirection of the sublist is unnecessary: just pass the list itself to Collections.sort . 但是,子列表的间接访问是不必要的:只需将列表本身传递给Collections.sort

Here is what's wrong with your code 这里是什么你的代码错误

    Collections.sort(ar.subList(1, ar.size()));

You are trying to sort indices 1~N, so index 0 is not sorted. 您正在尝试某种指数1〜N,所以索引0排序。

What you should be doing is sort the entire ArrayList like this 您应该做的是像这样对整个ArrayList进行排序

    Collections.sort(ar);

This will sort the ArrayList<String> in ascending order. 这将以升序对ArrayList<String>进行排序。

Output 输出量

2015|07|21 15:26:20!946!0.0091001
2015|07|21 15:26:21!947!1.0091002
2015|07|21 15:27:20!977!0.0091002
2015|07|21 16:26:20!946!0.0091001
2015|07|22 15:26:20!947!1.0091002
2015|08|21 15:26:20!977!0.0091002
2016|07|21 15:26:20!947!-0.0091002

However, your code just sorts the string list in ascending order so if you have month/date/year in different order, it will not be sorted by time. 然而,你的代码只需按升序排序,所以如果你有不同的顺序月/日/年,它不会按时间排序的字符串列表。 You should be using Date/Time utils available in Java or other libraries like Apache. 您应该使用Java或其他库(例如Apache)中提供的Date / Time utils。

You are sorting only a view of a sublist of your list, which will have no effect on the list you actually want to return. 您只排序列表的子列表的视图,这对您实际要返回的列表没有影响。 Indeed, List.sublist returns a new list. 确实, List.sublist返回一个列表。

By the way, your code could be much shorter : 顺便说一句,您的代码可能会更短:

List<String> list = Arrays.asList(
    "2016|07|21 15:26:20!947!-0.0091002", "2015|08|21 15:26:20!977!0.0091002", "2015|07|22 15:26:20!947!1.0091002",
    "2015|07|21 16:26:20!946!0.0091001", "2015|07|21 15:27:20!977!0.0091002", "2015|07|21 15:26:21!947!1.0091002", "2015|07|21 15:26:20!946!0.0091001");
Collections.sort(list);

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

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