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.
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Sorting all array elements except one
- Row wise sorting in 2D array
- Sorting array using Stacks
- Sorting array except elements in a subarray
- Sorting Array Elements By Frequency | Set 3 (Using STL)
- Sorting array of strings (or words) using Trie
- Sorting array elements with set bits equal to K
- Pair with largest sum which is less than K in the array
- Sorting array of strings (or words) using Trie | Set-2 (Handling Duplicates)
- Closest product pair in an array
- Find pair with greatest product in array
- Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted
- Sort an array of pairs using Java Pair and Comparator
- Find a pair with maximum product in array of Integers
Improved By : cs_abhi