Added a generic sort method that takes care of conditional logic