/** Sort the passed list according to dependencies previously set with * {@link #addDependency(Object, Object)}. Where possible, ordering will be * preserved if no dependency * @param list The list to be sorted. */ public void sort(Collection<T> list) { List<T> sorted = new ArrayList<>(); Set<T> visited = new HashSet<>(); Comparator<T> comparator = new InitialOrderComparator<>(list); // Visit all items in the list for (T t : list) visit(t,visited,sorted,comparator); list.clear(); list.addAll(sorted); }
/** Sort the passed array according to dependencies previously set with * {@link #addDependency(Object, Object)}. Where possible, ordering will be * preserved if no dependency * @param array The array to be sorted. */ public void sort(T[] array) { List<T> sorted = new ArrayList<>(); Set<T> visited = new HashSet<>(); Comparator<T> comparator = new InitialOrderComparator<>(array); // Visit all items in the array for (T t : array) visit(t,visited,sorted,comparator); sorted.toArray(array); }
/** Sort the passed list according to dependencies previously set with * {@link #addDependency(Object, Object)}. Where possible, ordering will be * preserved if no dependency * @param list The list to be sorted. */ public void sort(Collection<T> list) { List<T> sorted = new ArrayList<>(); Set<T> visited = new HashSet<>(); Comparator<T> comparator = new InitialOrderComparator<>(list); // Visit all items in the list for (T t : list) visit(t,visited,sorted,comparator); list.clear(); list.addAll(sorted); }
/** Sort the passed array according to dependencies previously set with * {@link #addDependency(Object, Object)}. Where possible, ordering will be * preserved if no dependency * @param array The array to be sorted. */ public void sort(T[] array) { List<T> sorted = new ArrayList<>(); Set<T> visited = new HashSet<>(); Comparator<T> comparator = new InitialOrderComparator<>(array); // Visit all items in the array for (T t : array) visit(t,visited,sorted,comparator); sorted.toArray(array); }