c++ - How to sort multidimentional array -


new coding ! after many attempts use sort without success, here actual code :

#include <algorithm> #define n   5       int a[n] = { 3, 6, 2, 4, 1 };      int b[n] = { 6, 3, 1, 2, 9 };     int c[n][3];      ( size_t = 0; < n; i++ )     {         c[i][0] = a[i]; c[i][1] = b[i]; c[i][2] = i;     }      ( size_t = 0; < n; i++ ) printf( "%d,%d, %d\n", c[i][0], c[i][1], c[i][2] );  

actual output :

3,6,0 6,3,1 2,1,2 4,2,3 1,9,4 

and need sort first key , outputtexpected should :

1,9,4 2,1,2 3,6,0 4,2,3 6,3,1 

i tried desperate sort(c, c + n); see im beginer..

use qsort version

#include <cstdio> #include <cstdlib>  #define n   5  using namespace std;  int cmp(const void  *a, const void *b){     int *x = (int*)a;     int *y = (int*)b;     return (*x > *y) - (*x < *y); }  int main(){     int a[n] = { 3, 6, 2, 4, 1 };      int b[n] = { 6, 3, 1, 2, 9 };     int c[n][3];      ( size_t = 0; < n; i++ )     {         c[i][0] = a[i]; c[i][1] = b[i]; c[i][2] = i;     }     qsort(c, n, sizeof(*c), cmp);     ( size_t = 0; < n; i++ ) printf( "%d,%d, %d\n", c[i][0], c[i][1], c[i][2] );  } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -