finished sort function