finish sort function