Fixed and tested sort method.