finish the sort function