Cars: Mater National Championship expects that when two identical controllers are connected and then the first controller is unplugged and plugged back in, it gets the same device index that it had before. If it gets a different index, the controller does not work until the game is restarted.
As it stands without this patch, when the last device in the list is unplugged, its index becomes available for reuse, but if the last device is never unplugged, none of the indexes of the other devices formerly in the list can be reused. It doesn't make sense to make all index reuse depend on the last device. Instead, let's keep the device list sorted by index and reuse any free index immediately.