Finished sorting function