public sealed interface OrderBy
Specifies an order by clause for entity queries.
OrderBy instances define how query results should be sorted, supporting multiple columns, ascending/descending order, null value handling, and case-insensitive sorting for strings.
OrderBy can be used in entity definitions as default ordering, or in queries for custom sorting:
// Default ordering for the entity
Customer.TYPE.as()
.attributes(
Customer.LASTNAME.as()
.column()
.caption("Last name"),
Customer.FIRSTNAME.as()
.column()
.caption("First name"))
.orderBy(OrderBy.builder()
.ascending(Customer.LASTNAME, Customer.FIRSTNAME)
.build())
.build();
// Query usage examples
// Simple ascending sort
List<Entity> customers = connection.select(
Select.all(Customer.TYPE)
.orderBy(OrderBy.ascending(Customer.LASTNAME)));
// Multiple columns, mixed directions
List<Entity> tracksByRatingAndName = connection.select(
Select.all(Track.TYPE)
.orderBy(OrderBy.builder()
.descending(Track.RATING) // Highest rated first
.ascendingIgnoreCase(Track.NAME) // Case-insensitive names
.build()));
// With null handling
List<Entity> tracksByComposer = connection.select(
Select.all(Track.TYPE)
.orderBy(OrderBy.builder()
.ascending(OrderBy.NullOrder.NULLS_LAST, Track.COMPOSER)
.build()));
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceBuilds aOrderByinstance.static enumSpecifies how to handle null values during order by.static interfaceSpecifies an order by column and whether it's ascending or descending -
Method Summary
Modifier and TypeMethodDescriptionstatic OrderByCreates an ascending OrderBy for the given columns.static OrderBy.Builderbuilder()Creates aOrderBy.Builderinstance.static OrderBydescending(Column<?>... columns) Creates a descending OrderBy for the given columns.
-
Method Details
-
orderByColumns
List<OrderBy.OrderByColumn> orderByColumns()- Returns:
- the order by columns comprising this order by clause
-
builder
Creates aOrderBy.Builderinstance.- Returns:
- a
OrderBy.Builderinstance
-
ascending
Creates an ascending OrderBy for the given columns.// Single column ascending OrderBy byName = OrderBy.ascending(Artist.NAME); // Multiple columns ascending OrderBy byLastAndFirstName = OrderBy.ascending(Customer.LASTNAME, Customer.FIRSTNAME); // Usage in queries List<Entity> customers = connection.select( Select.all(Customer.TYPE) .orderBy(OrderBy.ascending(Customer.LASTNAME))); // Usage in entity definition as default ordering Customer.TYPE.as() .attributes( Customer.LASTNAME.as() .column() .caption("Last name"), Customer.FIRSTNAME.as() .column() .caption("First name")) .orderBy(OrderBy.ascending(Customer.LASTNAME, Customer.FIRSTNAME)) .build();- Parameters:
columns- the columns to order by ascending- Returns:
- a new ascending OrderBy instance based on the given columns
-
descending
Creates a descending OrderBy for the given columns.// Single column descending OrderBy byDateDescending = OrderBy.descending(Invoice.DATE); // Multiple columns descending OrderBy byRatingAndPlayCount = OrderBy.descending(Track.RATING, Track.PLAY_COUNT); // Usage - most recent invoices first List<Entity> recentInvoices = connection.select( Select.all(Invoice.TYPE) .orderBy(OrderBy.descending(Invoice.DATE))); // Combine with conditions List<Entity> recentCustomerInvoices = connection.select( Select.where(Invoice.CUSTOMER_FK.equalTo(customer)) .orderBy(OrderBy.descending(Invoice.DATE)));- Parameters:
columns- the columns to order by descending- Returns:
- a new descending OrderBy instance based on the given columns
-