Yassine Badache
Yassine Badache

Reputation: 1851

Why does Hibernate sorts my list that way?

I want to sort a list using Hibernate's Criterias, but don't understand how the framework sorts the results. I have a list of strings to query and sort. The values are as follow:

I want to sort them in ascending order. I expect this result: 1, 2, Mega, Mega, powerup, Super, Super.

However, I end up with: Mega, Mega, powerup, Super, Super, 1, 2.

I first thought it was because the ASCII Table, however uppercase and lowercases are treated at the same level (despite lowercases having a higher ASCII address).


The only thing I saw in my code that could potentially be relevant is this line:

// I don't quite understand this line
criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

Other than that, there's no sorting. We call criteria.list() to get our results.


Is it normal ? Did I miss something when understanding the way Hibernate works ? How do I get the result I want ?

Upvotes: 0

Views: 118

Answers (1)

Davide D'Alto
Davide D'Alto

Reputation: 8206

It's not Hibernate ORM that's doing the sorting, it's your database. When you use criteria, Hibernate ORM will create the query adding the proper order by clause to the SQL.

Depending on the database you are using, there is usually a way to define the collation and specify how strings and characters will be ordered.

For example, in PostgreSQL you can define it when you create the table:

CREATE TABLE test1 (
    a text COLLATE "de_DE",
    b text COLLATE "es_ES",
    ...
);

Upvotes: 1

Related Questions