Updated sort function.