Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 1024
void Sort(char str[][SIZE], size_t strCount);
int main(int argc, char* argv[])
{
char strList[][SIZE] = {"ZZZZZ", "BBB", "AAA", "MMMM"};
Sort(strList, 4);
for(int i = 0; i < 4; ++i)
printf("%s\n", strList[i]);
return 0;
}
//Simple selection sort
void Sort(char str[][SIZE], size_t strCount)
{
//Loop through all the elements
for(unsigned int i = 0; i < strCount; ++i)
{
//Set smallest to current element
char* smallest = str[i];
//Get the smallest string (starting after current element)
for(unsigned int ii = i + 1; ii < strCount; ++ii)
{
if(strcmp(str[ii], smallest) < 0)
smallest = str[ii];
}
char temp[sizeof(*str)];
//Copy smallest into temp
strncpy(temp, smallest, sizeof(*str));
//Swap current element into the smallest
strncpy(smallest, str[i], sizeof(*str));
//Swap the temp into the current element
strncpy(str[i], temp, sizeof(*str));
}
}