SortOrder Class
TheSortOrder class defines how data and delete files should be ordered in an Apache Iceberg table.
Package: org.apache.iceberg
Overview
TheSortOrder class provides:
- Multi-column sorting specifications
- Custom sort directions (ascending/descending)
- Null ordering control (nulls first/last)
- Sort order evolution with order IDs
- Transform-based sorting
Sort order ID 0 is reserved for unsorted tables. All custom sort orders must use IDs greater than 0.
Basic Methods
schema()
orderId()
fields()
isSorted()
isUnsorted()
Sort Order Comparison
satisfies(SortOrder anotherSortOrder)
- The other order is unsorted, OR
- This order has at least as many fields as the other, AND
- Each field in the other order is satisfied by the corresponding field in this order
SortOrder
required
The sort order to check against
sameOrder(SortOrder anotherSortOrder)
SortOrder
required
The sort order to compare
Conversion
toUnbound()
Static Factory Methods
unsorted()
builderFor(Schema schema)
Schema
required
The table schema
Builder Class
TheBuilder class creates valid sort orders.
Builder Methods
asc(Term term)
Term
required
An expression term (e.g.,
Expressions.ref("column"))asc(Term term, NullOrder nullOrder)
Term
required
An expression term
NullOrder
required
Null order (NULLS_FIRST or NULLS_LAST)
desc(Term term)
Term
required
An expression term
desc(Term term, NullOrder nullOrder)
Term
required
An expression term
NullOrder
required
Null order (NULLS_FIRST or NULLS_LAST)
sortBy(String name, SortDirection direction, NullOrder nullOrder)
String
required
The column name
SortDirection
required
Sort direction (ASC or DESC)
NullOrder
required
Null order (NULLS_FIRST or NULLS_LAST)
sortBy(Term term, SortDirection direction, NullOrder nullOrder)
Term
required
An expression term
SortDirection
required
Sort direction (ASC or DESC)
NullOrder
required
Null order (NULLS_FIRST or NULLS_LAST)
withOrderId(int newOrderId)
int
required
The order ID (must be > 0 for sorted orders)
caseSensitive(boolean sortCaseSensitive)
boolean
required
True for case-sensitive matching
build()
ValidationException if the sort order is invalid
Static Validation Methods
checkCompatibility(SortOrder sortOrder, Schema schema)
- All source fields exist in the schema
- All source fields are primitive types
- All transforms can be applied to their source types
SortOrder
required
The sort order to validate
Schema
required
The schema to validate against
ValidationException if incompatible
Usage Examples
Creating an Unsorted Order
Creating a Simple Sort Order
Creating a Multi-Column Sort Order
Custom Null Ordering
Sort with Transforms
Using sortBy Method
Updating Table Sort Order
Checking Sort Order Satisfaction
Inspecting Sort Fields
Sort Order Evolution
Case-Insensitive Builder
Enums
SortDirection
NullOrder
Best Practices
Source Code Reference
Source:org/apache/iceberg/SortOrder.java:41