如何在java中对ResultSet进行排序?

cod*_*441 1 java database jdbc

顺便说一下,我不能在db中做ORDER BY

Jon*_*eet 9

Extract the results into a List<YourResultType> and use Collections.sort(). If you only ever need to sort in one "natural" order, then implement Comparable<T> in the result type itself... otherwise implement Comparator<T> once per sort order, and pass an instance of the relevant comparator to Collections.sort().

  • @ user521180:你没有.重新设计您的解决方案 已经很糟糕了,你不能将数据库用于其设计的工作之一...... (5认同)

And*_*yle 8

ORDER BY在数据库中.

你应该重新评估为什么你不能这样做.如果有人问"如何用锤子插入螺丝?我不能使用螺丝刀",不要说服他们说螺丝刀在第一种情况下是正确的解决方案是不负责任的.

如果你真的,真的无法原生地订购结果集,那你就不走运了.它只是来自数据库的一个流,所以你必须把它全部读成一个临时的List,对那个集合进行排序,然后从那里开始.对于较小的结果集,这可能不是一个问题,但对于较大的结果集,这可能会对效率造成很大影响.