public 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:

 public class Store extends DefaultDomain {

     interface Customer {
         EntityType TYPE = DOMAIN.entityType("store.customer");
         Column<String> LAST_NAME = TYPE.stringColumn("last_name");
         Column<String> FIRST_NAME = TYPE.stringColumn("first_name");
         Column<LocalDate> BIRTH_DATE = TYPE.localDateColumn("birth_date");
         Column<Boolean> ACTIVE = TYPE.booleanColumn("active");
     }

     void defineCustomer() {
         // Default ordering for the entity
         Customer.TYPE.define(
                 Customer.LAST_NAME.define()
                     .column(),
                 Customer.FIRST_NAME.define()
                     .column(),
                 Customer.BIRTH_DATE.define()
                     .column(),
                 Customer.ACTIVE.define()
                     .column())
             .orderBy(OrderBy.builder()
                 .ascending(Customer.LAST_NAME, Customer.FIRST_NAME)
                 .build())
             .build();
     }
 }

 // Query usage examples
 // Simple ascending sort
 List<Entity> customers = connection.select(
     Select.where(all(Customer.TYPE))
         .orderBy(OrderBy.ascending(Customer.LAST_NAME))
         .build());

 // Multiple columns, mixed directions
 List<Entity> customersByActiveAndName = connection.select(
     Select.where(all(Customer.TYPE))
         .orderBy(OrderBy.builder()
             .descending(Customer.ACTIVE)  // Active customers first
             .ascendingIgnoreCase(Customer.LAST_NAME, Customer.FIRST_NAME)  // Case-insensitive names
             .build())
         .build());

 // With null handling
 List<Entity> customersByBirthDate = connection.select(
     Select.where(all(Customer.TYPE))
         .orderBy(OrderBy.builder()
             .ascending(OrderBy.NullOrder.NULLS_LAST, Customer.BIRTH_DATE)
             .build())
         .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(Customer.NAME);
      
       // Multiple columns ascending
       OrderBy byNameAndEmail = OrderBy.ascending(Customer.LAST_NAME, Customer.FIRST_NAME);
      
       // Usage in queries
       List<Entity> customers = connection.select(
           Select.where(all(Customer.TYPE))
               .orderBy(OrderBy.ascending(Customer.LAST_NAME))
               .build());
      
       // Usage in entity definition as default ordering
       Customer.TYPE.define(
               Customer.LAST_NAME.define()
                   .column(),
               Customer.FIRST_NAME.define()
                   .column())
           .orderBy(OrderBy.ascending(Customer.LAST_NAME, Customer.FIRST_NAME))
           .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 byDateDesc = OrderBy.descending(Order.ORDER_DATE);
      
       // Multiple columns descending
       OrderBy byPriorityAndDate = OrderBy.descending(Task.PRIORITY, Task.DUE_DATE);
      
       // Usage - most recent orders first
       List<Entity> recentOrders = connection.select(
           Select.where(all(Order.TYPE))
               .orderBy(OrderBy.descending(Order.ORDER_DATE))
               .build());
      
       // Combine with conditions
       List<Entity> recentCustomerOrders = connection.select(
           Select.where(Order.CUSTOMER_FK.equalTo(customer))
               .orderBy(OrderBy.descending(Order.ORDER_DATE))
               .build());
      
      Parameters:
      columns - the columns to order by descending
      Returns:
      a new descending OrderBy instance based on the given columns