Created
October 22, 2012 01:44
-
-
Save dndx/3929214 to your computer and use it in GitHub Desktop.
C Sort Array of Struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
typedef struct { | |
char a; | |
char b; | |
} s; | |
int comp (const s *a, const s *b) { | |
return a->a > b->a || a->b < b->b ? -1 : 1; | |
} | |
int main (void) { | |
s arr[] = { | |
{5, 5}, {6, 2}, {2, 7}, {5, 2}, {1, 0}, {5, 1}, {6, 1}, {2, 9} | |
}; | |
qsort(&arr, sizeof(arr) / sizeof(s), sizeof(s), (int (*) (const void *, const void *)) &comp); | |
for (int i=0; i < sizeof(arr) / sizeof(s); i++) { | |
printf("{%d, %d}\n", arr[i].a, arr[i].b); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment