The best way to achieve this is to use a struct like this:
1 2 3 4 5 6
struct data {
int a;
int b;
};
data ab[5];
At first sort the array ab with any sorting algorithm with respect to the value of b. After that you just need to sort the array again using a stable sorting algorithm for the a component.