Fix Spark-983 and support exteranl sorting for sortByKey