Skip to content

Instantly share code, notes, and snippets.

@caloni
Created November 11, 2020 00:59
Show Gist options
  • Save caloni/5fe2767dbb36f769fe5e24ffd54d2c3f to your computer and use it in GitHub Desktop.
Save caloni/5fe2767dbb36f769fe5e24ffd54d2c3f to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmpstr(void const *a, void const *b)
{
const char const *aa = *(const char const **)a;
const char const *bb = *(const char const **)b;
return strcmp(aa, bb);
}
int main()
{
char *names[] = {
"Liam", "Olivia", "Noah", "Emma",
"Oliver", "Ava", "William", "Sophia",
"Elijah", "Isabella", "James", "Charlotte",
"Benjamin", "Amelia", "Lucas", "Mia",
"Mason", "Harper", "Ethan", "Evelyn",
};
size_t names_len = sizeof(names) / sizeof(char*);
int i;
qsort(names, names_len, sizeof(char*), cmpstr);
for( i = 0; i < names_len; ++i )
{
printf("%s\n", names[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment