RoleGrantedAuthorityComparator implements Comparator<RoleGrantedAuthority> { @Override public int compare(RoleGrantedAuthority rga1, RoleGrantedAuthority rga2) { GrantedAuthorityTarget target1 = rga1.getTarget(); GrantedAuthorityTarget target2 = rga2.getTarget(); Type type1 = target1.getType(); Type type2 = target2.getType(); int result = ((Integer) type1.ordinal()).compareTo(type2.ordinal()); if (result != 0) { return result; } String targetId1 = target1.getTargetId(); String targetId2 = target2.getTargetId(); result = targetId1.compareToIgnoreCase(targetId2); if (result != 0) { return result; } return rga1.getRoleName().compareToIgnoreCase(rga2.getRoleName()); }  @Override int compare(RoleGrantedAuthority rga1, RoleGrantedAuthority rga2);  }
@Test public void compare() { RoleGrantedAuthority rga1 = new RoleGrantedAuthority( new GrantedAuthorityTarget("project", GrantedAuthorityTarget.Type.PROJECT), "role1"); RoleGrantedAuthority rga2 = new RoleGrantedAuthority( new GrantedAuthorityTarget("project/branch", GrantedAuthorityTarget.Type.BRANCH), "role2"); RoleGrantedAuthority rga3 = new RoleGrantedAuthority( new GrantedAuthorityTarget("project/branch2", GrantedAuthorityTarget.Type.BRANCH), "role3"); RoleGrantedAuthority rga4 = new RoleGrantedAuthority( new GrantedAuthorityTarget("project/branch2", GrantedAuthorityTarget.Type.BRANCH), "role4"); List<RoleGrantedAuthority> expected = Lists.newArrayList(rga1, rga2, rga3, rga4); List<RoleGrantedAuthority> rgas = Lists.newArrayList(expected); while (rgas.equals(expected)) { Collections.shuffle(rgas); } RoleGrantedAuthorityComparator comparator = new RoleGrantedAuthorityComparator(); Collections.sort(rgas, comparator); assertEquals(expected, rgas); }