Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用不同的值对ArrayList进行排序_Java_Sorting_Arraylist - Fatal编程技术网

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

Java 如何使用不同的值对ArrayList进行排序,java,sorting,arraylist,Java,Sorting,Arraylist,我有一个ArrayList,我正在使用下面的代码对它进行排序,这里的sortField是string类型的,工作非常好,但有时它也可以是Date类型的字段,下面的代码在这种情况下引发了类强制转换异常。需要使用什么以使其在两种场景中都能工作。提前谢谢你的帮助 Collections.sort(tempList, new BeanComparator(sortField, String.CASE_INSENSITIVE_ORDER)); 下面是堆栈跟踪 2014-08-26 16:58:18,57

我有一个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)

我们可能需要该错误的全文及其堆栈跟踪来确定发生了什么。java.util.Date的可能副本是否无法转换为java.lang.String给您一个提示?对不同的类类型使用不同的比较器,或者使BeanComparator通用。我猜它是强类型的StringTo,您可以提供您正在进行排序的比较器和pojo类吗。