Finished sorting function

Date confirmed needs to be tweaked