Function to sort indexes in ascending order and switch the code to keep the effective positions same
See Implementation
Function to sort indexes in ascending order and switch the code to keep the effective positions same