[refactoring] Generalized import resorting algorithm.