Sorting to get the right answer