Move the empty list check before sorting to avoid processing an empty collection and provide clearer error handling.