Improve import order script in api

Please read commits message for details. It should apply cleanly to both 2-1-stable and 2-0-stable.