SemVer implements Comparable<SemVer> { @Override public int compareTo(SemVer other) { Iterator<String> i1 = this.versions.iterator(); Iterator<String> i2 = other.versions.iterator(); int level = 4; while (i1.hasNext() && i2.hasNext()) { int v = compareComp(i1.next(), i2.next()); if (v > 0) { return level; } else if (v < 0) { return -level; } if (level > 2) { level--; } } if (i1.hasNext()) { return level; } else if (i2.hasNext()) { return -level; } i1 = this.preRelease.iterator(); i2 = other.preRelease.iterator(); if (!i1.hasNext() && i2.hasNext()) { return 1; } if (i1.hasNext() && !i2.hasNext()) { return -1; } while (i1.hasNext() && i2.hasNext()) { int v = compareComp(i1.next(), i2.next()); if (v > 0) { return 1; } else if (v < 0) { return -1; } } if (i1.hasNext()) { return 1; } if (i2.hasNext()) { return -1; } if (this.metadata != null) { if (other.metadata != null) { int v = this.metadata.compareTo(other.metadata); if (v > 0) { return 1; } else if (v < 0) { return -1; } } else { return 1; } } else if (other.metadata != null) { return -1; } return 0; } SemVer(String v); boolean hasNpmSnapshot(); boolean hasPreRelease(); boolean hasPrefix(SemVer other); @Override int compareTo(SemVer other); @Override boolean equals(Object that); @Override int hashCode(); @Override String toString();  }
@Test public void testMixedPrerelease() { SemVer v2a3a = createVersion("1.0.0-2a-3a", true, false); SemVer v2a3 = createVersion("1.0.0-2a-3", true, false); SemVer v1234 = createVersion("1.0.0-1234", true, false); SemVer v911 = createVersion("1.0.0-911", true, false); assertEquals(1, v2a3a.compareTo(v2a3)); assertEquals(1, v2a3.compareTo(v1234)); assertEquals(1, v1234.compareTo(v911)); assertEquals(-1, v911.compareTo(v2a3a)); }