improve sort function