Sort an array by values using a user-defined comparison function