Refactored sorting comparison functions.