Sorting before returning data