From 4b75c336bb591f99acfcbfe27c50b7594ba00b3d Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Mon, 7 Mar 2016 11:42:41 +0100 Subject: [PATCH] Sha256Hash: Fix compareTo() not being consistent with equals(). --- .../src/main/java/org/bitcoinj/core/Sha256Hash.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/core/Sha256Hash.java b/core/src/main/java/org/bitcoinj/core/Sha256Hash.java index 88cb0f35..793424e4 100644 --- a/core/src/main/java/org/bitcoinj/core/Sha256Hash.java +++ b/core/src/main/java/org/bitcoinj/core/Sha256Hash.java @@ -266,8 +266,15 @@ public class Sha256Hash implements Serializable, Comparable { } @Override - public int compareTo(Sha256Hash o) { - // note that in this implementation compareTo() is not consistent with equals() - return this.hashCode() - o.hashCode(); // arbitrary but consistent + public int compareTo(final Sha256Hash other) { + for (int i = 31; i >= 0; i--) { + final int thisByte = this.bytes[i] & 0xff; + final int otherByte = other.bytes[i] & 0xff; + if (thisByte > otherByte) + return 1; + if (thisByte < otherByte) + return -1; + } + return 0; } }