diff --git a/core/src/main/scala/org/apache/spark/util/collection/ExternalAppendOnlyMap.scala b/core/src/main/scala/org/apache/spark/util/collection/ExternalAppendOnlyMap.scala
index d615767284c0b0b9190c24cdf6f90b127ce5032a..170f09be215344562a7ed3edbf38de9562806b78 100644
--- a/core/src/main/scala/org/apache/spark/util/collection/ExternalAppendOnlyMap.scala
+++ b/core/src/main/scala/org/apache/spark/util/collection/ExternalAppendOnlyMap.scala
@@ -337,8 +337,8 @@ class ExternalAppendOnlyMap[K, V, C](
       }
 
       override def compareTo(other: StreamBuffer): Int = {
-        // minus sign because mutable.PriorityQueue dequeues the max, not the min
-        -minKeyHash.compareTo(other.minKeyHash)
+        // descending order because mutable.PriorityQueue dequeues the max, not the min
+        if (other.minKeyHash < minKeyHash) -1 else if (other.minKeyHash == minKeyHash) 0 else 1
       }
     }
   }
@@ -422,7 +422,9 @@ class ExternalAppendOnlyMap[K, V, C](
 private[spark] object ExternalAppendOnlyMap {
   private class KCComparator[K, C] extends Comparator[(K, C)] {
     def compare(kc1: (K, C), kc2: (K, C)): Int = {
-      kc1._1.hashCode().compareTo(kc2._1.hashCode())
+      val hash1 = kc1._1.hashCode()
+      val hash2 = kc2._1.hashCode()
+      if (hash1 < hash2) -1 else if (hash1 == hash2) 0 else 1
     }
   }
 }