Extends in-memory-repository-sample with sorting support.
Adds a Comparator<Owner> parameter to the repository interface.
The caller controls the sort order — the repository applies it before returning results.
Passing null skips sorting and returns records in insertion order.
All code is in a single file: InMemoryRepositorySortingApp.java
InMemoryRepositorySortingApp.java
│
├── Owner # Entity — record with id and name
├── OwnerRepository # Repository interface with Comparator<Owner> parameter
├── FakeOwnerRepository # In-memory implementation using LinkedHashMap
└── InMemoryRepositorySortingApp # Entry point + demo()
Comparator as a parameter. The repository interface accepts a Comparator<Owner> — the caller decides the order, the repository just applies it.
Null means no sorting. Passing null returns records in their natural insertion order — no special case needed in the caller.
Original data is never mutated. Sorting is applied on a stream — the underlying LinkedHashMap stays unchanged.
Owner[id=1, name=jack1]
Owner[id=2, name=jack2]
Owner[id=3, name=jack3]
Owner[id=4, name=jack4]
Owner[id=5, name=jack5]
Owner[id=5, name=jack5]
Owner[id=4, name=jack4]
Owner[id=3, name=jack3]
Owner[id=2, name=jack2]
Owner[id=1, name=jack1]
Owner[id=1, name=jack1]
Owner[id=2, name=jack2]
Owner[id=3, name=jack3]
Owner[id=4, name=jack4]
Owner[id=5, name=jack5]
First block — sorted by id ascending. Second — by id descending. Third — no sorting, insertion order.
./mvnw spring-boot:run- Previous: in-memory-repository-sample
- Next: pagination-pageable-sample