1

My attempt to sort an ArrayList in reverse (descending) order with the following code snippet - used in a JSF managed bean to populate a dataGrid returns the error below:

public List<Surec> getPendingForms() {
   ...
   List<Surec> tersSiraliListe = new ArrayList<Surec>(someSet);
   Comparator<Surec> comparator = Collections.reverseOrder();
   Collections.sort(tersSiraliListe,comparator);
   return tersSiraliListe;
}

Exception:

...

Caused by: javax.el.ELException: /surecler/dashboard.xhtml @43,118 rendered="#{empty surecBean.pendingForms}": Error reading 'pendingForms' on type net.ozar.web.jsfmanaged.SurecBean
    at com.sun.facelets.el.TagValueExpression.getValue(TagValueExpression.java:76)
    at javax.faces.component.UIComponentBase.isRendered(UIComponentBase.java:390)
    ... 30 more
Caused by: java.lang.ClassCastException: net.ozar.entity.DerivedFromSurec cannot be cast to java.lang.Comparable
    at java.util.Collections$ReverseComparator.compare(Unknown Source)
    at java.util.Arrays.mergeSort(Unknown Source)
    at java.util.Arrays.sort(Unknown Source)
    at java.util.Collections.sort(Unknown Source)
    at net.ozar.web.jsfmanaged.SurecBean.getPendingForms(SurecBean.java:134)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:83)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:54)
    at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:123)
    at org.apache.el.parser.AstEmpty.getValue(AstEmpty.java:45)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
    at com.sun.facelets.el.TagValueExpression.getValue(TagValueExpression.java:71)
    ... 31 more

Since I defined comparator as an object of type Comparator<Surec> I don't understand why the exception is thrown. Anyone can help me with that?

1
  • 1
    It strikes me as pretty weird - and bad - that Collections.reverseOrder() is parameterised with a plain <T>, rather than a <T extends Comparable<T>>, since it can never actually work for non-comparable objects. Commented Apr 25, 2011 at 9:03

3 Answers 3

4

Does DerivedFromSurec implement Comparable ?

Unless you provide your own Comparator implementation, rather than the implementation given by Collections.reverseOrder(), you will need to implement the Comparable interface. The alternative is to supply your own Comparator.

Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that your DerivedFromSurec entities are not comparable. Your Surec class has to implement java.lang.Comparable.

Comments

0

Implement your own comparable interface (ascending as it should be) through your custom class if you are using one and then do the following: Collections.sort(list, Collections.reverseOrder());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.