optimization and error added case checking

no throws an exception if you pass it a non collection and added a more
efficient sort