I have a two dimensional array and want to sort both rows depending on the order of the elements in the first row:
i start with:
row1: { 4, 3, 1, 5, 0}
row2: { 7, 8, 9, 1, 2}
and the result should be:
row1: { 0, 1, 3, 4, 5}
row2: { 2, 9, 8, 7, 1}
QUESTION: Is it possible to achieve this, by using the qsort() function?
Not directly ...
... but qsort()
could sort vectors by each vector's first element.
So the example data needed to be transposed and be converted into a fake 2d array, where the root pointer points to an array of pointers, with each pointer pointing to a row of the transposed original data.
qsort()
then is passed the root pointer and the compare function compares on the vectors' first element. The vectors are passed to compare function by reference.
After sorting is done the result then needs to be converted the revers way as done prior to the call to qsort()
.