We are implementing data sort with the jpa data on one of the columns and our code is -
@Override
public Predicate toPredicate(Root<UpgradeFile> uf, CriteriaQuery<?> query, CriteriaBuilder cb) {
if (sortAttr != null && sortAttr.equals("version")) {
if (sortOrder == 1) { //asc order
orderList.add(cb.asc(uf.get(sortAttr)));
} else if (sortOrder == 2) { //desc order
orderList.add(cb.desc(uf.get(sortAttr)));
}
query.orderBy(orderList);
}
...
our version column is in string and has values like "11.0.2.73" , "11.0.2.312". As column is in string sorting does not work properly so we are required to cast it as numeric after removing "." for example -
order by ( CAST(
replace(version, '.', '')
AS numeric) ) asc;
My question is how can we incorporate this query in the above jpa code in either orderby or in criteriabuilder.