Suppose we have three arrays a, b, and c:
int a[1000] = {3,1,5,4};
int b[1000] = {7,9,11,3};
char c[1000] = {'A','B','C','D'};
Array a is then sorted, so it becomes:
a == {1,3,4,5}
Is it possible to arrange for the other two arrays to have their elements rearranged by index, so that they mirror the placement of the sorted elements in the sorted array a? In this example, this should result in
b == {9,7,3,11}
c == {'B','A','D','C'}
How can I achieve this?
you can create a class
ABC, which will hold 3 fields: int a, int b, char c.implement the
operator<for this class, and create aABC[]of the approppriate size and populate it such thatABC[i] = ABC(a[i],b[i],c[i]).implement the
operator<so it will compare onlya, and use sort on the ABC array.after done sorting, you have all your elements in the desired order, just iterate the ABC array and populate the other arrays.
EDIT:
simplified [and hard coded] code sample:
works good for me on codepad: http://codepad.org/eCyNkyqR