You want to sort in reverse path order, except when one path extends another. A simple work around is to add a high-ascii valued character ("z" is convenient) to the end of the path before comparing:
Collections.sort(mods, new Comparator<TextModification>() {
@Override
public int compare(TextModification mod1, TextModification mod2) {
int pathComp = (mod2.getPath() + "z").compareTo(mod1.getPath() + "z");
return pathComp == 0 ? mod2.getPriority() - mod1.getPriority() : pathComp;
}
});
Note also the simplification of the code using a ternary operator and a priority difference.