List sorted = new LinkedList();      List from = new ArrayList(moduleDescriptors);  for (int i=from.size()-1; i>=0; i--) { ModuleDescriptor md = (ModuleDescriptor)from.get(i);  List after = new LinkedList();  List between = new LinkedList();    int place = 0;  for (ListIterator it2 = sorted.listIterator(); it2.hasNext();) { ModuleDescriptor smd = (ModuleDescriptor)it2.next(); if (md.dependsOn(smd)) { place = it2.nextIndex(); between.addAll(after); after = new LinkedList(); } else if (smd.dependsOn(md)) { after.add(smd); } } sorted.add(place, md); if (!between.isEmpty()) { sorted.removeAll(between); sorted.addAll(place+1-between.size(), between); } }  return sorted;
return ModuleDescriptorSorter.sortModuleDescriptors(moduleDescriptors);
    public static List sortModuleDescriptors(Collection moduleDescriptors) {        // Note that classical Comparator do not work here, because        // one to one comparison is not suffisant since we only use        // direct dependencies and not transitive one        List sorted = new LinkedList();                // we iterate over the original list reversely, so that last traversed are the first in order        // since we place md by default at the beginning of the result, this preserves the order        // if it doesn't need to be changed (no dependency relationship between mds).                List from = new ArrayList(moduleDescriptors); // copy to be able to traverse reversely                for (int i=from.size()-1; i>=0; i--) {            ModuleDescriptor md = (ModuleDescriptor)from.get(i);            // a list of sorted mds that should be after the current md            List after = new LinkedList();            // a list of sorted mds that should be after current md and which are not            List between = new LinkedList();                          // find its place in current sorted list                        int place = 0;            // let's check dependency relation ship with others            for (ListIterator it2 = sorted.listIterator(); it2.hasNext();) {                ModuleDescriptor smd = (ModuleDescriptor)it2.next();                if (md.dependsOn(smd)) { // it depends on another, it has to be placed after                    place = it2.nextIndex();                    between.addAll(after);                    after = new LinkedList();                } else if (smd.dependsOn(md)) {                    after.add(smd);                }            }            sorted.add(place, md);            if (!between.isEmpty()) {                sorted.removeAll(between);                sorted.addAll(place+1-between.size(), between);            }        }                return sorted;    }