EntitySorter { public static List<EntityDto> sortByInheritance(List<EntityDto> list) { List<EntityDto> sorted = new ArrayList<>(list.size()); for (Iterator<EntityDto> iterator = list.iterator(); iterator.hasNext(); ) { EntityDto entity = iterator.next(); if (entity.isBaseEntity()) { sorted.add(entity); iterator.remove(); } } while (!list.isEmpty()) { for (Iterator<EntityDto> iterator = list.iterator(); iterator.hasNext(); ) { final EntityDto entity = iterator.next(); EntityDto superClass = (EntityDto) CollectionUtils.find(sorted, new Predicate() { @Override public boolean evaluate(Object object) { return object instanceof EntityDto && ((EntityDto) object).getClassName().equals(entity.getSuperClass()); } }); if (null != superClass) { sorted.add(entity); iterator.remove(); } } } return sorted; } private  EntitySorter(); static List<EntityDto> sortByHasARelation(List<EntityDto> allEntities, SchemaHolder schemaHolder); static List<EntityDto> sortByInheritance(List<EntityDto> list);  }
@Test public void shouldProperlySortByInheritance() { List<EntityDto> initialList = new ArrayList<>(); initialList.add(childEntity); initialList.add(parentEntity); List<EntityDto> entities = EntitySorter.sortByInheritance(initialList); Assert.assertTrue(entities.indexOf(parentEntity) < entities.indexOf(childEntity)); }