I'm learning and understanding Java now, and while practising with arrays I had a doubt. I wrote the following code as an example:
class example
{
public static void main(String args[])
{
String a[] = new String[] {"Sam", "Claudia", "Josh", "Toby", "Donna"};
int b[] = new int[] {1, 2, 3, 4, 5};
for(int n=0;n<5;n++)
{
System.out.print (a[n] + "...");
System.out.println (b[n]);
}
System.out.println (" ");
java.util.Arrays.sort(a);
for(int n=0;n<5;n++)
{
System.out.print (a[n] + "...");
System.out.println (b[n]);
}
}
In a nutshell, this class created two arrays with five spaces each. It fills one with names of characters from the West Wing, and fills the other with numbering from one to five. We can say that the data in these two strings corresponds to each other.
Now, the program sorts the array with the names in it using Arrays.sort()
. After printing the array again, you can see that while the names are now in alphabetical order, the numbers do not correspond anymore as the second array is unchanged.
How can I shuffle the contents of the second array to match the sort requirements of the first? The solution must also be flexible to allow for changes in the scope and size of the program. Please do not post any answers asking me to change my methodology with the arrays, or propose a more 'efficient' way of doing things. This is for educational purposed and I'd like a straight solution to the example code provided. Thanks in advance!
EDIT: I do NOT want to create an additional class, however I think some form of sorting through nested loops might be an option instead of Arrays.sort().
Here is the answer for your query.
What you want is not possible because you don't know internally how
Arrays.sort
swap the elements in your String array, so there is no way to swap accordingly the elements in the int array.You should create a class that contains the
String
name and theint
position as parameter and then sort this class only with the name, providing a custom comparator toArrays.sort
.If you want to keep your current code (with 2 arrays, but this not the ideal solution), don't use
Arrays.sort
and implement your own sorting algorithm. When you swap two names, get the index of them and swap the two integers in the other array accordingly.Below is the code without using any
Map
Collection, but if you want to useMap
then it becomes very easy. Add both the arrays into map and sort it.You have to ZIP your two arrays into an array which elements are instances of a class like:
And sort that array with a custom comparator.
Your code should be something like:
The arrays are not linked in any way. Like someone pointed out take a look at
SortedMap
http://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.htmlTreeMap
http://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.htmlSome people propose making a product type. That is feasible only if the amount of elements is small. By introducing another object you add object overhead (30+ bytes) for each element and a performance penalty of a pointer (also worsening cache locality).
Solution without object overhead
Make a third array. Fill it with indices from
0
tosize-1
. Sort this array with comparator function polling into the array according to which you want to sort.Finally, reorder the elements in both arrays according to indices.
Alternative solution
Write the sorting algorithm yourself. This is not ideal, because you might make a mistake and the sorting efficiency might be subpar.