Difference between orderBy of hibernate and Collections.sort()

Rajshree Rai :

What is the difference between using order by of hibernate and Collections.sort() of Java? The time complexity of Collections.sort() is O(nlogn) and using orderBy must be O(1). However, the db processing must have some overhead too. Is it okay to use Collections.sort() in cases where using orderBy is not possible?

Stefan :

You can use either one. Generally they should both be O(nlogn) (since the database also has to apply a sorting algorithm and it is not better than the sorting algorithm in Collections).

I would use the database's orderBy if possible (*), since you then can limit the amount of data that are sent from the database (at least in cases where you'r only interested in the first part of the matching data).

(*) There are situations where you want you want to implement a "weird" sort order (ie "sort by the 2nd letter of the third word"), and then it is better to do it in Java, but in most cases orderBy is sufficient and hence preferrable.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=284653&siteId=1