Skip to content

Instantly share code, notes, and snippets.

@dndx
Created October 22, 2012 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dndx/3929214 to your computer and use it in GitHub Desktop.
Save dndx/3929214 to your computer and use it in GitHub Desktop.
C Sort Array of Struct
#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