repartitionAndSortWithinPartitions

fun <K, V> JavaRDD<Tuple2<K, V>>.repartitionAndSortWithinPartitions(partitioner: Partitioner): JavaRDD<Tuple2<K, V>>
fun <K, V> JavaRDD<Tuple2<K, V>>.repartitionAndSortWithinPartitions(partitioner: Partitioner, comp: Comparator<K>): JavaRDD<Tuple2<K, V>>

Repartition the RDD according to the given partitioner and, within each resulting partition, sort records by their keys.

This is more efficient than calling JavaRDD.repartition and then sorting within each partition because it can push the sorting down into the shuffle machinery.