Fix JIRA-983 and support exteranl sort for sortByKey