Strangley JavaPairRDD doesn't have sortWithinPartition but instead it expects repartitionAndSortWithinPartition. To implement this I had to write an Identical partitioner which returns the same partition for records in same partition.
Do we have any other better way to solve this?