ResourceComparator implements Comparator<String> { @Override public int compare(String s1, String s2) { if (s1.equals(s2)) { return 0; } int s1Order = ordering.indexOf(s1); int s2Order = ordering.indexOf(s2); if (s1Order == -1 && s2Order == -1) { return s1.compareTo(s2); } if (s1Order != -1 && s2Order != -1) { return s1Order - s2Order; } return s1Order == -1 ? 1 : -1; }  @Override int compare(String s1, String s2);  }
@Test public void testCompare() { Map<String, Object> map = new TreeMap<>(new ResourceComparator()); map.put("a", "zzzzz"); map.put("name", 1); map.put("z", "fdsfdsds"); map.put("d", new ArrayList<>()); map.put("id", 1); map.put("e", false); map.put("c", 1); map.put("href", "dfdfgdf"); map.put("b", new HashMap<>()); map.put("description", 1); map.put("f", 20); map.put("type", 1); Iterator<String> iter = map.keySet().iterator(); assertEquals(iter.next(), "href"); assertEquals(iter.next(), "name"); assertEquals(iter.next(), "id"); assertEquals(iter.next(), "description"); assertEquals(iter.next(), "type"); assertEquals(iter.next(), "a"); assertEquals(iter.next(), "b"); assertEquals(iter.next(), "c"); assertEquals(iter.next(), "d"); assertEquals(iter.next(), "e"); assertEquals(iter.next(), "f"); assertEquals(iter.next(), "z"); }