I currently have a system in place which can filter and sort records in the database and return them as a Paged object. One of the lines is like this:
final PageRequest request = new PageRequest(this.pagingSettings.getPageNumber(),
this.pagingSettings.getPageSize(), sortDirection, sortedBy);
This works correctly, but now I'm having the following situation. I'm trying to sort on a house number, which is a varchar in my Postgres database. For example, we have 1, 12, 111, 1004 but also 1A or 36-BASEMENT. When sorting on these (character) values, these would sort on: 1, 1004, 111, 12, 1A, ...
So, sortedBy
is now a String, which in this case is houseNumber
. I found out that using the ORDER BY
argument ... ORDER BY NULLIF(regexp_replace(container_number, E'\\D', '', 'g'), '')::int";
in Postgres, the sorting would be exactly like I wanted: 1, 1A, 12, 111, ...
However, just changing the sortedBy
String to sortedBy = "NULLIF(regexp_replace(container_number, E'\\D', '', 'g'), '')::int";
does not seem to work.
Does anyone have a suggestion on how to sort the character values in a PageRequest numerical, without changing the database?