It isn’t random, it goes by the order of the colours in the palettes.
You can swap palettes by going to RGB mode, changing the palette, and going to Indexed again. That will do the “smart” conversion you want.
In Indexed mode, the index-based behaviour is usually what is needed, so it wouldn’t make sense to change that IMO.