I have a list of type Object[]
inside it are TreeMap.Entry<String, ChatDisplayDayWrapper>
.
The key is a string like 2017-11-28
, 2017-11-27
, 2017-11-26
.
How can I sort the list to get the keys sorted reversed also
--> 2017-11-26
, 2017-11-27
, 2017-11-28
?
I have tried to convert the String
to Date
put I do not know how to write them back after sorting the list.
Code
Object[] objectList = chatWrapper.getChatDayWrappers();
List<Date> listToSort = new ArrayList<Date>();
for (int i = 0; i <= objectList.size() - 1; i++) {
String day = objectList[i].key;
Date newDate = dfDate.parse(day);
listToSort.add(newDate)
}
getLogger().info("before sorting listToSort: " + listToSort);
listToSort = listToSort.sort { a, b ->
a <=> b
}
getLogger().info("After sorting listToSort: " + listToSort);
output
before sorting listToSort:
[Tue Nov 28 00:00:00 CET 2017, Mon Nov 27 00:00:00 CET 2017, Sun Nov 26 00:00:00 CET 2017]
After sorting listToSort:
[Sun Nov 26 00:00:00 CET 2017, Mon Nov 27 00:00:00 CET 2017, Tue Nov 28 00:00:00 CET 2017]
screenshot