Skip to main content

5.2 Dynamic Sorting

Dynamic sorting refers to the sorting method being dynamically specified by the client.

Use Strings Directly

The simplest way for the client to specify sorting is strings.

The string formats supported by Jimmer are as follows:

store.name asc, name asc, edition desc or store.name asc; name asc; edition desc

Where store.name means first inner join the associated object BookStore through the Book.store property, and then sort by the name property of the associated object.

The path length is unlimited, but the association type along the way needs to be one-to-one or many-to-one.

String sortCode = ...Omitted...;

BookTable table = BookTable.$;
List<Book> books = sqlClient
.createQuery(table)
.orderBy(Order.makeOrders(table, sortCode))
.select(table)
.execute();

Transfer via Spring Sort

Sometimes, when developing in combination with Spring Data, org.springframework.data.domain.Sort may be used to represent dynamic sorting.

At this point, developers need to perform two steps:

I. Convert string to Sort Object

The easiest uploading way for the client is still the string discussed earlier, like store.name asc, name asc, edition desc or store.name asc; name asc; edition desc.

Therefore, first the string needs to be converted to the org.springframework.data.domain.Sort object.

After importing the dependency org.babyfish.jimmer:jimmer-spring-boot-starter:$version, the static method org.babyfish.jimmer.spring.model.SortUtils.toSort can then be used, as follows:

String sortCode = ...Omitted...;
Sort sort = SortUtils.toSort(sortCode);

II. Sort using Sort object

After importing the dependency org.babyfish.jimmer:jimmer-spring-boot-starter:$version:

  • Java: Use the utility method org.babyfish.jimmer.spring.repository.SpringOrders.toOrders to convert the org.springframework.data.domain.Sort object into a collection of ordering objects accepted by Jimmer.

  • Kotlin: Directly use the orderBy extension function that accepts org.springframework.data.domain.Sort object.

org.springframework.data.domain.Sort sort = ...Omitted...;

BookTable table = BookTable.$;
List<Book> books = sqlClient
.createQuery(table)
.orderBy(SpringOrders.toOrders(table, sort))
.select(table)
.execute();