Matthias Schippling
Matthias Schippling

Reputation: 2953

Why does JPA "order by ascending" returns upper case entries first?

I have a criteria query with the following code:

CriteriaQuery<MyClass> criteria = cb.createQuery(MyClass.class);
Root<MyClass> root = criteria.from(MyClass.class);
criteria = criteria
        .where(cb.like(root.get(MyClass_.name), name + "%"))
        .orderBy(cb.asc(root.get(MyClass_.name)))
        .select(root);

Why is my example result ordered this way:

abcdE
abcde

and not like this:

abcde
abcdE

and how can I fix that?

I would expect the result that's completely lower case to be returned first, but that's not the case.

Upvotes: 0

Views: 757

Answers (2)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

Unicode character points for A-Z are from 65 to 90, while for a-z are from 97 to 122. So, the ordering is right from the database.

To solve this (because this is not an issue but a different requirement you have to fulfill) simply don't sort the data using JPA but manually by using Collections#sort and provide a custom Comparator<MyClass>.

Upvotes: 1

Jens
Jens

Reputation: 69450

Because in char code for uppercase letter are before lower case letter.

Upvotes: 1

Related Questions