findAll() vs findAll(Sort) in JpaRepository
Fast recall
Quick revision
Core answer
Whether you push sorting to the database (ORDER BY) instead of loading all rows and sorting in memory, and whether you know how to express multi-field and directional sorts
Calling findAll().stream().sorted(...) which loads the whole table into memory and fails to scale once the table grows
Mention multi-field sorting, Sort.Direction.DESC, ignoreCase(), combining with PageRequest, and the need for an index on sorted columns
Interview-ready guide
Interview answer
repository.findAll() can return rows in any order the database chooses. repository.findAll(Sort.by("name").ascending().and(Sort.by("id"))) requests a stable name-then-id order. The id tie-breaker matters when several rows share a name. Sorting in the database is the correct semantic choice for pagination because sorting a single Java page cannot establish the order of rows that were not fetched. An index may help, but the optimizer can still choose an explicit sort.
For example, findAll() is acceptable for a tiny fixed lookup table. For an orders screen, I would request PageRequest.of(0, 20, Sort.by("createdAt").descending()) so ordering is explicit and memory use is bounded.
findAll() vs findAll(Sort) in JpaRepository
Direct Answer
findAll() has no defined result order unless the query includes ORDER BY; insertion order must not be assumed. findAll(Sort) asks the database to order by the named entity properties. Database sorting is not automatically faster than Java sorting—indexes, result size, memory, pagination, and the query plan matter—but it is required when ordering must happen before paging.
Common Mistakes & Pitfalls
- Sorting in Java —
findAll().stream().sorted(...)loads the entire table into memory and collapses at scale. - Unstable order — relying on
findAll()to return insertion order; withoutORDER BYit isn't guaranteed. - No index — sorting on an unindexed column forces a full scan and sort every call.
- Forgetting tie-breakers — single-field sorts can shift page boundaries; chain a second field for stable ordering.
Check your understanding
Answer these without looking back at the article.
- How do you sort by multiple fields in Spring Data?
- Can you accept Sort parameters from a REST request in Spring Boot?
- What is the default sort order when no Sort is specified?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.