In this article, we will discuss a problem where we are given two arrays, an integer array, and a character array. The task is to sort the character array according to the values of the integer array. This means that if we sort the integer array in increasing order, the values in the character array must be arranged accordingly.
Let's understand this problem with an example. Suppose we have two arrays a[] = {3, 1, 2} and b[] = {'G', 'E', 'K'}. To sort the character array according to the integer array, we first sort the integer array in increasing order, i.e., {1, 2, 3}. Now, the value corresponding to 1 in the original array is 'E', similarly for 2 and 3, the values of the character array are 'K' and 'G' respectively. Therefore, the output will be {E, K, G}.
Similarly, if we have another input where a[] = {4, 1, 3, 2} and b[] = {'A', 'X', 'B', 'Y'}, the sorted character array according to the integer array will be {'X', 'Y', 'B', 'A'}.
Now let's see how to solve this problem. The solution is similar to the problem "Keeping indexes after sorting the array". The idea is to create a group of items where, in each group, the first element is from the integer array, and the second element of the group is an element from the character array corresponding to the same index as the chosen integer value.
We can create a class to represent a group as stated above and create an ArrayList of class to represent N such groups for N items in both integer and character arrays. Finally, we sort the ArrayList of class according to the integer value in the group. We then traverse the ArrayList and print all character values which are now arranged according to the increasing order of integer values.
Let's understand the above approach with the help of code. We create a class "Pair" to represent the groups and initialize two variables in it, first and second. The first variable is of integer type and the second variable is of character type. We also create a class "MyCmp" that implements the Comparator interface. We override the compare() method of Comparator to sort the ArrayList based on the first variable of the Pair class.
In the main method, we take two input arrays, an integer array "a" and a character array "b". We call the method "sortAccordingToOther()" and pass the arrays a[] and b[] as arguments. This method creates an array of Pair objects and initializes it with elements from both the input arrays. It then sorts the array of Pair objects using the Collections.sort() method and passing the object of the MyCmp class. Finally, it traverses the sorted array of Pair objects and assigns the second element of each Pair object to the character array b[].
Here is the complete Java code for the above approach:
javaimport java.util.*;
class Pair {
int first;
char second;
Pair(int f, char s)
{
first = f;
second = s;
}
}
class MyCmp implements Comparator<Pair>
{
public int compare(Pair p1, Pair p2)
{
return p1.first - p2.first;
}
}
class SortCharacterArray {
static void sortAccordingToOther(int a[], char b[])
{
int n = a.length;
Pair arr[] = new Pair[n];
for(int i=0; i<n; i++)
{
arr[i] = new Pair(a[i], b[i]);
}