Further implementation in the sort function