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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4379592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:06:21  来源:igfitidea点击:

How can I sort ResultSet in java?

javadatabasejdbc

提问by code511788465541441

I can't do ORDER BY in the db by the way

顺便说一下,我不能在数据库中执行 ORDER BY

回答by Jon Skeet

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().

将结果提取到 a 中List<YourResultType>并使用Collections.sort(). 如果您只需要按一种“自然”顺序进行排序,则Comparable<T>在结果类型本身中实现……否则Comparator<T>每个排序顺序实现一次,并将相关比较器的实例传递给Collections.sort().

回答by Andrzej Doyle

You do ORDER BYin the DB.

ORDER BY在数据库中做。

You should reassess whyyou can't do this. If someone asked "how do I insert a screw with a hammer? I can't use a screwdriver by the way", it would be irresponsible not to persuade them that the screwdriver was the right solution in the first instance.

你应该重新评估为什么你不能这样做。如果有人问“我如何用锤子插入螺丝?顺便说一下,我不能使用螺丝刀”,首先不说服他们螺丝刀是正确的解决方案是不负责任的。

If you really, reallycan't order the result set natively, you're out of luck. It's just a stream from the database, so you'd have to read it all into a temporary List, sort that collection and then go from there. For small result sets this isn't likely to be a problem, but for large ones this will likely impose quite a hit on efficiency.

如果您真的,真的无法对结果集进行本地排序,那么您就不走运了。它只是来自数据库的一个流,所以你必须将它全部读入一个临时的List,对该集合进行排序,然后从那里开始。对于小的结果集,这不太可能成为问题,但对于大的结果集,这可能会对效率造成相当大的影响。

回答by Aaron McIver

Move the data out of the ResultSet into whatever object representation you want and then sort the data just as you would any other data at that point.

将 ResultSet 中的数据移到您想要的任何对象表示中,然后像在此时对任何其他数据进行排序一样对数据进行排序。

If you make use of Collections.Sort to perform your sorting on a complex object you will need to implement Comparator.

如果使用 Collections.Sort 对复杂对象执行排序,则需要实现Comparator

回答by gifpif

It is possible to sort it in the database before you run the query and then store in resultSet.

可以在运行查询之前在数据库中对其进行排序,然后将其存储在 resultSet 中。

SELECT * FROM tableName ORDER BY columnName ASC

SELECT * FROM tableName ORDER BY columnName ASC

Is something like this that you want?

你想要这样的东西吗?

回答by mezmo

It looks like you would have to be responsible for implementing the ResultSet interface with a custom object that would give you the functionality you're looking for....sorry.

看起来您必须负责使用自定义对象实现 ResultSet 接口,该对象将为您提供您正在寻找的功能......抱歉。

回答by mike

This can be solved in the query itself. You order by a calculated column. Your calculated column converts the natural value to a value that can be sorted numerically or alphabetically. You might define a "convert" function and call that function in the order by clause.

这可以在查询本身中解决。您按计算列排序。您的计算列将自然值转换为可以按数字或字母顺序排序的值。您可以定义一个“转换”函数并在 order by 子句中调用该函数。

At some level the natural to numeric conversion must happen whether in your code or in the database. An algorithm is an algorithm no matter where it runs.

在某种程度上,无论是在您的代码中还是在数据库中,都必须进行自然到数字的转换。一个算法无论在哪里运行都是一个算法。