How can I order by integer in a string in Spring JPA's CriteriaBuilder?

596 views Asked by At

My data consists of strings that can be numbers or strings ("12" should be seen as a number while "12REF" should be seen as a string).

I am looking to implement an order by in my criteriaBuilder that sorts the strings by numbers first, and puts the strings sorted alphabetically at the end.

Correctly sorted example: <[["1", "2", "10", "A", "AB", "B", "DUP", "LNE", "NUL"]]>

Currently my code looks like this (just sorting by asc, using the CriteriaQuery's orderBy).

.orderBy(QueryUtils.toOrders(
                            Sort.by(Sort.Direction.ASC, selection.getAlias()), root,
                            criteriaBuilder)
                            );

which results in: <[["1", "10", "2", "A", "AB", "B", "DUP", "LNE", "NUL"]]>

How can I implement a custom ordering here?

Edit: the data is stored in a MySQL database

0

There are 0 answers