recursively sorts a list