There is a list of objects - List<Contact> contacts = new ArrayList<Contact>(); The method that returns it:

 public List<Contact> getContacts() { return contacts; } 

It is required to sort it in reverse order:

 Collections.sort(cm.getContacts(), Collections.reverseOrder()); 

Throws an exception:

 xception in thread "main" java.lang.ClassCastException: com.sirma.Contact cannot be cast to java.lang.Comparable at java.util.Collections$ReverseComparator.compare(Collections.java:5108) at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355) at java.util.TimSort.sort(TimSort.java:220) at java.util.Arrays.sort(Arrays.java:1512) at java.util.ArrayList.sort(ArrayList.java:1454) at java.util.Collections.sort(Collections.java:175) at com.sirma.Main.main(Main.java:77) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 

Please tell me how to solve the problem

Closed due to the fact that off-topic participants zRrr , Alexey Shimansky , cheops , aleksandr barakin , Yuriy SPb ♦ 12 Jun '16 at 23:13 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - zRrr, Alexey Shimansky, cheops, aleksandr barakin, Yuriy SPb
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    and in contacts at least one element is? Did the Contact class implement the compareTo method? as? And what did you write Collections.reverseOrder() for? - Alexey Shimansky
  • if the order of elements in the reverse order is an iterator, if you sort the elements then yes - sort. - Mikhail Kolomiets
  • Yes, you just need to reverse the order - Alexander Dermenzhi
  • Questions asking for help with debugging (“why does this code not work?”) Should include the desired behavior, a specific problem or error, and the minimum code for playing it right in the question .. There is no description of the Contact class, nor the implementation of its methods in the current question for sorting, neither information about how full the collection, nor about other elements. - Alexey Shimansky

1 answer 1

Collections.reverse()

 static class A { String bar; public A(String bar) { this.bar = bar; } @Override public String toString() { return "A [bar=" + bar + "]"; } } List<A> list = Arrays.asList(new A[] { new A("3"), new A("22"), new A("9") }); Collections.reverse(list); System.out.println(list); // [A [bar=9], A [bar=22], A [bar=3]]