HostToken implements Comparable<Long> { @Override public int compareTo(Long o) { return this.token.compareTo(o); } HostToken(Long token, Host host); Long getToken(); Host getHost(); @Override String toString(); @Override int hashCode(); @Override boolean equals(Object obj); @Override int compareTo(Long o); int compareTo(HostToken o);  }
@Test public void testSort() throws Exception { HostToken t1 = new HostToken(1L, new HostBuilder().setHostname("foo1").setPort(1234).setRack("foo_rack").createHost()); HostToken t2 = new HostToken(2L, new HostBuilder().setHostname("foo2").setPort(1234).setRack("foo_rack").createHost()); HostToken t3 = new HostToken(3L, new HostBuilder().setHostname("foo3").setPort(1234).setRack("foo_rack").createHost()); HostToken t4 = new HostToken(4L, new HostBuilder().setHostname("foo4").setPort(1234).setRack("foo_rack").createHost()); HostToken t5 = new HostToken(5L, new HostBuilder().setHostname("foo5").setPort(1234).setRack("foo_rack").createHost()); HostToken[] arr = {t5, t2, t4, t3, t1}; List<HostToken> list = Arrays.asList(arr); Assert.assertEquals(t5, list.get(0)); Assert.assertEquals(t2, list.get(1)); Assert.assertEquals(t4, list.get(2)); Assert.assertEquals(t3, list.get(3)); Assert.assertEquals(t1, list.get(4)); Collections.sort(list, new Comparator<HostToken>() { @Override public int compare(HostToken o1, HostToken o2) { return o1.compareTo(o2); } }); Assert.assertEquals(t1, list.get(0)); Assert.assertEquals(t2, list.get(1)); Assert.assertEquals(t3, list.get(2)); Assert.assertEquals(t4, list.get(3)); Assert.assertEquals(t5, list.get(4)); }