How to sort a multidimensional array by qsort function from #cstdlib? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

How to sort a multidimensional array by qsort function from #cstdlib?

For example {{1,6},{2,3},{3,8},{4,0}} -> {{4,0},{2,3},{1,6},{3,8}} Now it's sorted by 1 index How to write a comparator for this? Comparator for a simple array int compare(const void * x1, const void * x2){ return (*(int*)x1-*(int*)x2); } I'm not sure at all how it works

12th Apr 2020, 11:33 PM
Quarz
1 Antwort
+ 2
int comp(const void *x1, const void *x2) { return *((int*)x1+1) > *((int*)x2+1) ; } int arr[][2] = {{1,6}, {2,3}, {3,8}, {4,0}}; qsort(arr, 4, sizeof arr[0], comp);
13th Apr 2020, 1:02 AM
rodwynnejones
rodwynnejones - avatar