简体   繁体   中英

Sorting List of String array by 3rd element

I am trying to sort a List of String arrays by its 3rd element. The 3rd element is a date value. I've been trying to solve this for hours now by swapping but still can't get this solved. For my current List , I don't believe Arrays.sort() would be ideal that's why I am sorting this via loop.

List<String[]> Record = new ArrayList<>();
Record.add(new String[] { "0001047166", "11047161", "20191223", "20200916" });
Record.add(new String[] { "0001047166", "11047162", "20191223", "20200916" });
Record.add(new String[] { "0001047166", "11047163", "20191222", "20200916" });
Record.add(new String[] { "0001047166", "11047166", "20191218", "20200916" });
Record.add(new String[] { "0001047359", "11047359", "20191217", "20200917" });

int size = Record.size();
int swapIndex = 0;
for (int i = 0; i < size-1; i++) {
    String[] currentArray = Record.get(i); //get array on current index
    String date = currentArray[2]; //get the date on 3rd position, index 2 of current array
    swapIndex = i;
    for (int j = i + 1; j < Record.size(); j++) {
    String[] nextArray = Record.get(j);  //get the next array (i + 1)
        String otherDate = nextArray[2]; //get the date on 3rd position, index 2 of next arrray
        if (date.compareTo(otherDate) > 0) {
            String[] currArray = currentArray; //save the current array to temp
        //swap 
            Record.set(swapIndex, nextArray); 
            Record.set(j, currArray);
            swapIndex++;
        }
    }           
}

//Display result
for (String[] a : Record) {
    System.out.println(a[2]);
}

Output I get is

20191222 (wrong)
20191217
20191218
20191223
20191223

Desired output:

20191217
20191218
20191222
20191223
20191223

I'd appreciate any ideas or suggestions.

Thank you.

You may sort using a lambda with a custom comparator which uses the third (string) date:

Record.sort((String[] sa1, String[] sa2) -> sa1[2].compareTo(sa2[2]));
Record.stream().forEach(sa -> System.out.println(sa[2]));

This prints:

20191217
20191218
20191222
20191223
20191223

Note that ideally you should be comparing some sort of bona-fide date types, and not date strings. That being said, given that your date strings appear to be in an ISO format, sorting as text should work. Also, Java naming convention holds that variable names should not begin with uppercase letters. So, ideally your list should be called record and not Record .

Collections.sort will work here (or List.sort ):

Collections.sort(Record, Comparator.comparing(array -> array[2]));
Record.sort(Comparator.comparing(array -> array[2]));

And if your Java is older than 8, you still can use Collections.sort :

Collections.sort(Record, new Comparator<String[]>() {
    @Override
    public int compare(String[] o1, String[] o2) {
        return o1[2].compareTo(o2[2]);
    }
});

You do not need to parse into a Date object before comparing, because the format yyyyMMdd allows for String comparison to be consistent with Date's.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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