Implemented the sorting process
Fixed error checking the length