We are given two arrays. We need to sort one array according to another.
Input : 2 1 5 4 9 3 6 7 10 8 A B C D E F G H I J Output : 1 2 3 4 5 6 7 8 9 10 B A F D C G H J E I Here we are sorting second array (a character array) according to the first array (an integer array).
We have discussed different ways in below post.
Sort an array according to the order defined by another array
In this post we are focusing on using the pair container present in STL of C++.
To achieve our task we are going to make pairs of respective elements from both the arrays.Then simply use the sort function. The important thing to note is, the first element in the pairs should be from the array according to which the sorting is to be performed.
1 2 3 4 5 6 7 8 9 10 B A F D C G H J E I
This article is contributed by Vineet Joshi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sorting objects using In-Place sorting algorithm
- Sort an array according to the order defined by another array
- Print elements of an array according to the order defined by another array | set 2
- Sort the array of strings according to alphabetical order defined by another string
- Know Your Sorting Algorithm | Set 1 (Sorting Weapons used by Programming Languages)
- Know Your Sorting Algorithm | Set 2 (Introsort- C++’s Sorting Weapon)
- Sorting Array Elements By Frequency | Set 3 (Using STL)
- Check if a string can be transformed to another by sorting substrings
- Keep track of previous indexes after sorting a vector in C++ STL
- Sorting a Map by value in C++ STL
- std::tuple, std::pair | Returning multiple values from a function using Tuple and Pair in C++
- Sort an array according to absolute difference with a given value "using constant extra space"
- Sort an array according to absolute difference with given value using Functors
- Sort an array of strings according to string lengths using Map
- Pair formation such that maximum pair sum is minimized
- Stack of Pair in C++ STL with Examples
- Difference between pair in Multiset and Multimap in C++ STL
- Pair in C++ Standard Template Library (STL)
- Sorting array of strings (or words) using Trie
- Sorting array of strings (or words) using Trie | Set-2 (Handling Duplicates)
Improved By : cs_abhi