# How do you sort a 2 Dimensional Array?

This is a discussion on How do you sort a 2 Dimensional Array? within the C Programming forums, part of the General Programming Boards category; I was just wondering how do I sort a 2 dimensional array? char testArray[5][11]; strcpy( testArray[0], "hello"); strcpy( testArray[1], "Hello"); ...

1. ## How do you sort a 2 Dimensional Array?

I was just wondering how do I sort a 2 dimensional array?

char testArray[5][11];
strcpy( testArray[0], "hello");
strcpy( testArray[1], "Hello");
strcpy( testArray[2], "1hello");
strcpy( testArray[3], "2hello");
strcpy( testArray[4], "Ello");

Thus the sorted will be...
1hello
2hello
Ello
Hello
hello

Anyone know how to do this?

2. Figure out how to compare two strings with strcmp(), and then sort your array with some algorithm, like insert sort.

3. I got it to sort by ASCII order... But How do I sort by Alphabetical Ascii?

Such as...
instead of: A, B, C, D, a, b, c, d
it will be: A, a, B, b, C, c, D, d

4. Oh, nevermind, its sorted by Ascii.

Thanks for the suggestion of strcmp

5. In case you wanted to have in-case-sensitive string comparison, there are a few things you could do:
• Use the non-standard function stricmp(). Quite a few people recommend this, but I don't -- namely because it is not standard. Borland might have it, but that doesn't mean my compiler does.
• Convert all the strings to lower- or upper-case. Not good if you want to get the strings again with mixed case. (See what happened to DOS when they tried this! . . . .)
• Write your own string comparison function. This is the best idea. And really, it's not that hard.
Code:
```int case_insensitive_strcmp(const char *one, const char *two) {
char a, b;

do {
a = tolower(*one);
b = tolower(*two);

if(a < b) return -1;
else if(a > b) return 1;
} while(*one++ && *two++);

return 0;
}```