Suppose I have an Array A[]={8, 9, 11, 14, 16, 20}; which I have to sort according to another array B[]={6, 5, 1, 2, 4, 3};.
Sorted A would be A[]={20, 16, 8, 9, 14, 11};
So, How A will be sorted is told in B.
First element of B is the greatest so first element of A will also be the greatest. Third element of B is smallest so third element of A will also be the smallest
If B were something sorted Descending like {100, 84, 74, 51, 5, 1} Then A will also be sorted Descending.
Examples:
1. if B = {12, 8, 14, 156, 2, 84}, A would be {11, 9, 14, 20, 8, 16}
2. if B = {2, 3, 45, 0, 7, 56}, A would be {9, 11, 16, 8, 14, 20}
Its like if I have some friends of different ages, and I want to give them something according to their ages. Eldest person will get the biggest one... smaller than him will get the smaller one than that.. and so on.
I have seen similar questions but they are not like my problem.
My idea is to sort both first. And then rearrange.
Is there any fast solution?
Bspecifies the sorting order ofA. Check them please.Bdetermines the order ofA, please clarify your sorting criteria.