Finish sorting function