Sorting a multidimensional array

Hi all,

I would like to sort two arrays (two vectors) after the first and then after the second array in ascending order.

Is there a quick way?

Thanks,
grima
Can you explain what you're trying to achieve more in detail ?
Hi AleaIactaEst,

yes I can ;)

I have to arrays:

 ``12345`` ``````a[0] = 3; b[0] = 4; a[1] = 4; b[1] = 2; a[2] = 4; b[2] = 0; a[3] = 1; b[3] = 5; a[4] = 3; b[4] = 2;``````

and my goal is to sort the arrays this way:

 ``12345`` ``````a[0] = 1; b[0] = 5; a[1] = 3; b[1] = 2; a[2] = 3; b[2] = 4; a[3] = 4; b[3] = 0; a[4] = 4; b[4] = 2;``````

Thanks and best wishes,
grima
The best way to achieve this is to use a struct like this:

 ``123456`` ``````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.

For more detailed information about stable sorting algorithms see Wikipedia (http://en.wikipedia.org/wiki/Sorting_algorithm#Stability) or do a search for stable sorting algorithms.
Topic archived. No new replies allowed.