如何使用不同的值对ArrayList进行排序

时间:2014-08-26 21:01:15

标签: java sorting arraylist

我有一个ArrayList,我使用下面的代码对它进行排序,这里的sortField类型为string,工作得很好但有时它也可以是Date类型的字段,下面是抛出一个类转换异常这种情况。需要使用什么使它在两种场景中都能工作。非常感谢您的帮助。

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

下面是堆栈跟踪

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)

0 个答案:

没有答案