Sorting with a higher-order function