0

I have an ArrayList and I am using the below code to sort it, the sortField here is of type string which works perfectly fine but sometimes it can also be a field of type Date and the below is throwing a class cast exception in such a case. What needs to be used such that it works in both the scenarios. Thanks a lot for the help in advance.

Collections.sort(tempList, new BeanComparator(sortField, String.CASE_INSENSITIVE_ORDER));

Below is the stack trace

2014-08-26 16:58:18,573 ERROR -  -  - java.lang.ClassCastException: java.lang.ClassCastException: java.util.Date cannot be cast to java.lang.String
2014-08-26 16:58:18,573  INFO -  -  - Method execution failed: 
java.lang.ClassCastException: java.lang.ClassCastException: java.util.Date cannot be cast to java.lang.String
    at org.apache.commons.beanutils.BeanComparator.compare(BeanComparator.java:155)
    at java.util.TimSort.countRunAndMakeAscending(Unknown Source)
    at java.util.TimSort.sort(Unknown Source)
    at java.util.TimSort.sort(Unknown Source)
    at java.util.Arrays.sort(Unknown Source)
    at java.util.Collections.sort(Unknown Source)
    at com.test.org.web.ajax.File.listFiles(FileUpload.java:105)
    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 org.directwebremoting.impl.CreatorModule$1.doFilter(CreatorModule.java:229)
    at org.directwebremoting.impl.CreatorModule.executeMethod(CreatorModule.java:241)
    at org.directwebremoting.impl.DefaultRemoter.execute(DefaultRemoter.java:379)
    at org.directwebremoting.impl.DefaultRemoter.execute(DefaultRemoter.java:332)
    at org.directwebremoting.dwrp.BaseCallHandler.handle(BaseCallHandler.java:104)
    at org.directwebremoting.servlet.UrlProcessor.handle(UrlProcessor.java:120)
    at org.directwebremoting.servlet.DwrServlet.doPost(DwrServlet.java:141)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:643)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
Akshitha
  • 89
  • 1
  • 2
  • 7
  • We will probably need the full text of that error and its stack trace to figure out what is going on. – markspace Aug 26 '14 at 21:02
  • possible duplicate of [Sorted collection in Java](http://stackoverflow.com/questions/416266/sorted-collection-in-java) – Olimpiu POP Aug 26 '14 at 21:03
  • 2
    Does _java.util.Date cannot be cast to java.lang.String_ give you a hint? – Jim Garrison Aug 26 '14 at 21:11
  • Use different comparators for different class types, or make your BeanComparator generic. I'm guessing it's strongly typed to String – coffeeaddict Aug 26 '14 at 21:13
  • Can you provide ur comparator and pojo class on which you are doing the sort. – sbjumani Aug 26 '14 at 21:14
  • Does the following make sense to you: "could the youngest of all of you, case-insensitive, come with me"? That's what your code does. – JB Nizet Aug 26 '14 at 21:14
  • Why aren't you using parametrized array lists? Anyways, you need a Comparator for `Object` which checks `instanceof` *Date.class* or *String.class* and compare based on that. – EpicPandaForce Aug 26 '14 at 21:24

0 Answers0