Skip to content

Instantly share code, notes, and snippets.

@michaeljdennis
Last active May 17, 2018 03:08
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 michaeljdennis/ee36614cc7bc81f63aa08bbcc51a4234 to your computer and use it in GitHub Desktop.
Save michaeljdennis/ee36614cc7bc81f63aa08bbcc51a4234 to your computer and use it in GitHub Desktop.
Sort Example
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static int myCompare (const void * a, const void * b)
{
return strcmp (*(const char **) a, *(const char **) b);
}
void sort(const char *arr[], int n)
{
qsort (arr, n, sizeof (const char *), myCompare);
}
int main ()
{
const char *arr[] = {
"john",
"bobby",
"dear",
"test1",
"catherine",
"nomi",
"shinta",
"martin",
"abe",
"may",
"zeno",
"zack",
"angeal",
"gabby"
};
int n = sizeof(arr)/sizeof(arr[0]);
int i;
sort(arr, n);
for (i = 0; i < n; i++) {
printf("%d: %s \n", i, arr[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment