# Thread: Sort and compare 2D Array

1. ## Sort and compare 2D Array

DISCLAIMER: 1st post, please excuse any format violations...
Good evening folks.

Here's the deal. I have seven arrays to compare and sort in alphabetical order. My problem is this...

Input
box
fat
toy
red

All three letters (individually) are being sorted, so that it would look like this...
toy
rox
fet

My goal is to sort them alphabetically a------>z (as whole words).

Any suggestions? Thanks for the help!

Code:
```#include<stdio.h>
#include<string.h>

#define N_STRINGS 7
#define MAX_SIZE 80

void sort(char a[][MAX_SIZE], int n);

int main(void)
{
int i, n=N_STRINGS;
char a[N_STRINGS][MAX_SIZE];

printf("Enter 7 single character strings.\n\n");
for(i=0; i<n; ++i)
scanf("%s",&a[i]);

printf("The Strings you have entered:");//print the list of string characters
for(i = 0; i<n; ++i)
printf("%s\n",&a[i]);

sort(a, n);//sort um

printf("\nThe Stings sorted are:\n");//print the list of string characters
for(i = 0; i<n; ++i)
printf("%s\n",&a[i]);
return 0;
}

void sort(char a[][MAX_SIZE], int n)
{
int i, j, k;
char temp;

for (i = 0; i < n; i++)		{
for (j = i + 1; j < n; j++)		{
for (k = 0; k < n; k++)		{
if (strcmp(&a[j][k],&a[i][k])>0)	{
temp = a[i][k];
a[i][k] = a[j][k];
a[j][k] = temp;
}
}
}
}
}```

2. Well, since you're using string functions already, why don't you use strcmp, and strcpy to first compare entire words, and then copy them to the correct place?

1) Copy word N to a temporary array.
2) Copy word X to where word N is in the array.
3) Copy temporary array to where word X is in the array.

Quzah.

3. You can't use string comparison if you want to do individual letters. You should loop through a bit differently. Maybe something like:
Code:
```for(i = 0;a[0][i];i++)
for(j = 0;j < n;j++)
for(k = j+1;k < n;k++)
if(a[j][i] < a[k][i])
// Swap characters```
That's assuming all the strings are the same length.

EDIT: Nevermind. I thought you wanted to have them sorted all weird and was having problems with it. fet is a cool word.

4. > if (strcmp(&a[j][k],&a[i][k])>0) {
As Quzah said, what you want here is:
if (strcmp(a[j],a[i])>0) {

Then use strcpy() to swap a[i] and a[j]. And you don't need the k loop. Finally temp should be a char array.

5. Thanks for the advice! I'm up and running now...

I went with the strcpy route which seems to work well, however, how can you make it tolerate strings of different lengths, when you overwrite one array with another using the copy function?

Ex copy red to blue = blue
copy blue to yellow = blueow

Code:
```void sort(char a[][MAX_SIZE], int n)
{
int i, j;
char temp[1][MAX_SIZE];

for (i = 0; i < n; i++)		{
for (j = i + 1; j < n; j++)		{
if (strcmp(a[i], a[j]) > 0)	{
strcpy(temp[i], a[i]);
strcpy(a[i], a[j]);
strcpy(a[j], temp[i]);```

6. There's no need for temp to be 2-dimensional.
Code:
```void sort(char a[][MAX_SIZE], int n)
{
int i, j;
char temp[MAX_SIZE];

for (i = 0; i < n; i++)		{
for (j = i + 1; j < n; j++)		{
if (strcmp(a[i], a[j]) > 0)	{
strcpy(temp, a[i]);
strcpy(a[i], a[j]);
strcpy(a[j], temp);```

7. >how can you make it tolerate strings of different lengths
Your strings should already be terminated with a string terminator('\0'), so this shouldn't be a problem unless you are building each string char-by-char, in which case you should add the string terminator yourself.

8. I guess this was one of those situations where I was over-thinking this whole deal.

Thanks alot!

It's up and running, with problems <= 0

Whoohoo!