0

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

enter image description here

Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82
benz
  • 693
  • 1
  • 9
  • 29
  • What do you want to do after sorting them? Basically want the map to be in sorted order. Is that it? – Rao Nov 28 '17 at 09:57
  • 1
    A TreeMap is already sorted, so the entries are sorted (from https://stackoverflow.com/questions/47516828/java-util-treemapentry-entryset-is-applicable-for-argument-types-values) . Either reverse your sorting in the map or since you already have fully realized array, reverse that. Yet going from that `TreeMap` down to `Object[]` does more harm than good... – cfrick Nov 28 '17 at 10:11

2 Answers2

1

Java 8

 List<TreeMap.Entry<String, ChatDisplayDayWrapper>> myList = Arrats.asList(objectList);
 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
    Collections.sort(myList , (s1, s2) -> LocalDateTime.parse(s1.getKey(), formatter).
            compareTo(LocalDateTime.parse(s2.getKey(), formatter)));

Java 7

 List<TreeMap.Entry<String, ChatDisplayDayWrapper>> myList = Arrays.asList(objectList);
 static SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
public static Comparator<TreeMap.Entry<String, ChatDisplayDayWrapper>> myComparator = new Comparator<TreeMap.Entry<String, ChatDisplayDayWrapper>>() {

    @Override
    public int compare(TreeMap.Entry<String, TreeMap.Entry<String, ChatDisplayDayWrapper>>e1, Employee e2) {
        return formatter .parse(s1.getKey()).
            compareTo(formatter .parse(s2.getKey()))
    }
};

    Collections.sort(myList , myComparator);

I took it almost from here

Sergii Getman
  • 3,845
  • 5
  • 34
  • 50
  • DateTimeFormatter and LocalDateTime are in Java 8 I am working with Java 7 – benz Nov 28 '17 at 10:12
  • What about using SimpleDateFormat and Date? – Sergii Getman Nov 28 '17 at 10:19
  • I tried with this code ` Object[] objectList = chatWrapper.getChatDayWrappers(); List> myList = Arrays.asList(objectList); DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Collections.sort(myList , (s1, s2) -> Date.parse(s1.getKey(), formatter). compareTo(Date.parse(s2.getKey(), formatter)));` but I am getting `expecting ')', found ',' @ line 226, column 34. Collections.sort(myList , (s1, s2) -> Date.parse(s1.getKey(), formatter). ^ 1 error ` – benz Nov 28 '17 at 10:23
  • @tree man, you need just a tiny java-7 changes. Check update I've posted – Sergii Getman Nov 28 '17 at 11:00
0

Use like following .. for reverse sorting

     Comparator<Date> dateComparator = (a, b)-> a.compareTo(b);
     listToSort.sort(dateComparator.reversed());

for Natural sorting

     Comparator<Date> dateComparator = (a, b)->a.compareTo(b);
     listToSort .sort(dateComparator);
Pandey Amit
  • 657
  • 6
  • 19