Thanks a lot for your example. I would certainly take this approach and I am happy that you took the time to write it.
I feel a little sorry for you because I put a trap for the parent poster in the original code.
I wrote "a[j] < a[i]".
You "fixed" it while refactoring, which would be the good thing to do if I made a typo. If however I really wanted to sort circles differently from planets, then you made an error while refactoring what looked like copy-pasta but wasn't.
My original point was that when you "have" to copy-paste, you cannot clearly see what is or isn't part of the copy and what is new.
You found a way to rewrite the code without much copy- pasting, and I am happy to see that. Still, in other places where code duplication arise, there could be similar problems from a maintenance point of view.