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:
  • Method Details

    • orderByColumns

      List<OrderBy.OrderByColumn> orderByColumns()
      Returns:
      the order by columns comprising this order by clause
    • builder

      static OrderBy.Builder builder()
      Creates a OrderBy.Builder instance.
      Returns:
      a OrderBy.Builder instance
    • ascending

      static OrderBy ascending(Column<?>... columns)
      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

      static OrderBy descending(Column<?>... columns)
      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