An order makes an implicit comparison explicit. Define what ≤ means for domain values, then use that relationship to express ideas such as customer favorability, capability, classification, or valid state transitions. Ordering is not limited to sorting numbers or dates. section 7.7 section 7.7.1
A useful ordering treatment makes correctness visible through properties:
- Reflexivity means a ≤ a; antisymmetry means that a ≤ b and b ≤ a imply a = b; transitivity means that a ≤ b and b ≤ c imply a ≤ c. section 7.7.1
- Compose domain criteria with Java Comparator, using a primary business meaning and a secondary lexicographic tie-breaker. section 7.7
- Test the laws above the implementation by exercising every relevant state or every permutation and checking for one consistent result. section 7.7.1
Encoding valid task transitions
Model a lifecycle with sealed data: empty, Started, Completed, and Failed. Assign empty the lowest order, Started the next position, and terminal states the highest position. A transition is valid when the destination has a greater order than the current state, preventing self-updates and transitions out of terminal states. Enumerate every pair and verify both the ordering model and the database behavior. section 12.3
Listing 7.42 verifying ordering properties by checking all permutations
static <A> List<List<A>> permutations(A[] items) { #A
// ... #A
} #A
@Test
void anExerciseForTheReader() {
List<AuditFinding> expectedOrder = List.of(
INACCURATE, NO_ISSUE,
BILLING_ERROR, OUT_OF_COMPLIANCE);
assertTrue(
permutations(AuditFinding.values())
.stream()
.map(x -> x.stream().sorted(comparator).toList())
.allMatch(sorted -> sorted.equals(expectedOrder))); #B
}Use the permutation check to verify that every input arrangement produces the same expected sorted order. section 7.7.1









