All I want to do is to sort the array [P] from the highest to the lowest with the index values of the variables. When looking from this perspective, it seems easy. But there is another constraint that the first two will be sorted in themselves with the indexes and the last two will be sorted in themselves with the indexes but this time the highest of these two will get the index of 1 and the second will take 2. The written code below does not do this and I can not find where the error is. I am stuck with this code. Any help will be appreciated.

Thanks,

#include <stdio.h> #include <stdlib.h> #define M 2 // #define P 4 // int NItems[M]={2,2}; int cmp(const void *v1, const void *v2) { const int i1 = **(const int **)v1; const int i2 = **(const int **)v2; if(i1>i2) return -1; else return 1; } int main(void) { int array[P] = {10,20,5,15}; int *parray[P],i; for(int j=0;j<M;j++) {if(j==0) {for(i = (j*NItems[j]); i <=(j*NItems[j]+NItems[j]-1);i++) {parray[i] = &array[i]; } qsort(parray,NItems[j], sizeof *parray,cmp); } else for(i = (j*NItems[j-1]); i <=(j*NItems[j-1]+NItems[j]-1);i++) {parray[i] = &array[i]; } qsort(parray,NItems[j], sizeof *parray,cmp); } puts("Sorting the values:"); for(i=0;i<P;i++) printf("%d\n ",array[i]); for(int j=0;j<M;j++) {if(j==0) {for(i = (j*NItems[j]); i <=(j*NItems[j]+NItems[j]-1);i++) {printf("value: %-6d position: %d\n",*parray[i],(parray[i]-array)+1);} system("pause"); } else {for(i =(j*NItems[j-1]);i<=(j*NItems[j-1]+NItems[j]-1);i++) printf("value: %-6d position: %d\n",*parray[i],(parray[i]-array-NItems[j])+1);} system("pause"); } return 0; }