how to sort an array according to another array c++

C++
// Sort an array according to  
// other using pair in STL. 
#include <bits/stdc++.h> 
using namespace std; 
  
// Function to sort character array b[] 
// according to the order defined by a[] 
void pairsort(int a[], char b[], int n) 
{ 
    pair<int, char> pairt[n]; 
  
    // Storing the respective array 
    // elements in pairs. 
    for (int i = 0; i < n; i++)  
    { 
        pairt[i].first = a[i]; 
        pairt[i].second = b[i]; 
    } 
  
    // Sorting the pair array. 
    sort(pairt, pairt + n); 
      
    // Modifying original arrays 
    for (int i = 0; i < n; i++)  
    { 
        a[i] = pairt[i].first; 
        b[i] = pairt[i].second; 
    } 
} 
  
// Driver function 
int main() 
{ 
    int a[] = {2, 1, 5, 4, 9, 3, 6, 7, 10, 8}; 
    char b[] = {'A', 'B', 'C', 'D', 'E', 'F',  
                         'G', 'H', 'I', 'J'}; 
                           
    int n = sizeof(a) / sizeof(a[0]); 
      
    // Function calling 
    pairsort(a, b, n); 
  
    for (int i = 0; i < n; i++) 
        cout << a[i] << " "; 
    cout << endl; 
  
    for (int i = 0; i < n; i++) 
        cout << b[i] << " "; 
          
    return 0; 
} 

Source

Also in C++: