ConfigurationSourceComparator implements Comparator<ConfigurationSource>, Serializable { @Override public int compare(ConfigurationSource o1, ConfigurationSource o2) { int p1 = o1.getPrecedence(); int p2 = o2.getPrecedence(); if (p1 == p2) return 0; return p1 < p2 ? 1 : -1; }  @Override int compare(ConfigurationSource o1, ConfigurationSource o2);  }
@Test public void testSorting() { ConfigurationSource s1 = new ConfigurationSource() { @Override public void update() { } @Override public String getString(ConfigurationKey key) { return null; } @Override public int getPrecedence() { return 10; } @Override public Integer getInteger(ConfigurationKey key) { return null; } @Override public BigDecimal getBigDecimal(ConfigurationKey key) { return null; } @Override public Long getLong(ConfigurationKey key) { return null; } @Override public String[] getStringArray(ConfigurationKey key) { return null; } @Override public Map<String, Object> getItems() { return null; } @Override public Boolean getBoolean(ConfigurationKey key) { return null; } @Override public Date getDate(ConfigurationKey key) { return null; } }; ConfigurationSource s2 = new ConfigurationSource() { @Override public void update() { } @Override public String getString(ConfigurationKey key) { return null; } @Override public int getPrecedence() { return 0; } @Override public Integer getInteger(ConfigurationKey key) { return null; } @Override public BigDecimal getBigDecimal(ConfigurationKey key) { return null; } @Override public Long getLong(ConfigurationKey key) { return null; } @Override public String[] getStringArray(ConfigurationKey key) { return null; } @Override public Map<String, Object> getItems() { return null; } @Override public Boolean getBoolean(ConfigurationKey key) { return null; } @Override public Date getDate(ConfigurationKey key) { return null; } }; List<ConfigurationSource> sources = new ArrayList<>(); sources.add(s1); sources.add(s2); ConfigurationSourceComparator csc = new ConfigurationSourceComparator(); Collections.sort(sources, csc); Assert.assertEquals(s1, sources.get(0)); Assert.assertEquals(-1, csc.compare(s1, s2)); }