# Thread: Sorting 3 arrays alphabetically

1. ## Sorting 3 arrays alphabetically

Greetings everyone,
I am writing a program that allows the user to enter the first name, last name, and grade of each student. The program then sorts all the names alphabetically by last name, and display a list of all the students names and grades in alphabetical order.
I don't understand how can I associate to print out the first name and the points o that same array.
I managed to make the output sort them by last name and output them, too. But the names remain unswapped.
So when I input:
Angelina Zellweger 11
Suzy Applegate 55

it should give:
Suzy Applegate 55
Angelina Zellwegerr 11

but mine outputs this:
Angelina Applegate
Suzy Zellweger
Code:
```  for ( j = 0; j < cnum -1; j++){
posMin = j;

for (i = j+1; i < cnum; i++)
if (strcmp(last[i], last[posMin]) < 0)
posMin = i;

strcpy(tmp,last[j]);
strcpy(last[j],last[posMin]);
strcpy(last[posMin],tmp);
}

for (i = 0; i < cnum; i++)
printf("Last name is [%i] = %s\n",i,last[i]);```
That's the code for sorting the last name array.

Any help would be appreciated, thanks.

2. When you swap the last name, you need to swap the first name as well.

3. You need to swap the entire set of arrays at the same time...

Better plan would be to use a struct and simply swap structs around.

4. I agree, struct that contains first name, last name, and grade would be a better idea.

5. bithub, I realise that. As i said; I don't understand how can I associate to print out the first name and the points of that same array.

commontater, being new to C I would appreciate some easier commands :P is that in the family of strcpy and strcmp?

6. Originally Posted by Nephilim
bithub, I realise that. As i said; I don't understand how can I associate to print out the first name and the points of that same array.

commontater, being new to C I would appreciate some easier commands :P is that in the family of strcpy and strcmp?
Code:
```Code:
for ( j = 0; j < cnum -1; j++){
posMin = j;

for (i = j+1; i < cnum; i++)
if (strcmp(last[i], last[posMin]) < 0)
posMin = i;

strcpy(tmp,last[j]);
strcpy(last[j],last[posMin]);
strcpy(last[posMin],tmp);

// you also need to swap the first name and grade here

}

for (i = 0; i < cnum; i++)
printf("Last name is [%i] = %s\n",i,last[i]);```

7. I managed to sort the 3rd array of points but only if the points are entered in a descending order.
Code:
```    int j;
char tmp[20], tmp2[20];
int posMin, tmp3;
for (j=0; j<cnum-1; j++){
posMin = j;

for (i = j+1; i < cnum; i++)
if (strcmp(cognome[i], cognome[posMin]) < 0)
posMin = i;

strcpy(tmp,cognome[j]);
strcpy(cognome[j],cognome[posMin]);
strcpy(cognome[posMin],tmp);

strcpy(tmp2,nome[j]);
strcpy(nome[j],nome[posMin]);
strcpy(nome[posMin],tmp2);

for (i = j+1; i<cnum; i++)
{
if(punteggio[i]>punteggio[posMin])
{
tmp3=punteggio[j];
punteggio[j]=punteggio[posMin];
punteggio[posMin]=tmp3;
}
}
}```
But it doesn't work if it's not in any other order... help?

8. Why do you even have that loop you added at the end? All you need to do was to add a third set of copy lines, like the first two blocks of strcmp.

Quzah.

9. Originally Posted by Nephilim
I managed to sort the 3rd array of points but only if the points are entered in a descending order.
Code:
```    int j;
char tmp[20], tmp2[20];
int posMin, tmp3;
for (j=0; j<cnum-1; j++){
posMin = j;

for (i = j+1; i < cnum; i++)
if (strcmp(cognome[i], cognome[posMin]) < 0)
posMin = i;

strcpy(tmp,cognome[j]);
strcpy(cognome[j],cognome[posMin]);
strcpy(cognome[posMin],tmp);

strcpy(tmp2,nome[j]);
strcpy(nome[j],nome[posMin]);
strcpy(nome[posMin],tmp2);

for (i = j+1; i<cnum; i++)
{
if(punteggio[i]>punteggio[posMin])
{
tmp3=punteggio[j];
punteggio[j]=punteggio[posMin];
punteggio[posMin]=tmp3;
}
}
}```
But it doesn't work if it's not in any other order... help?
Specifically:
1) add int i to the variables.
2) Put curly braces around both your for loops, and your if statement.

If you indent subordinate lines just 3 spaces or so, it will be MUCH clearer. It should look like this: (this is a Substitution sort)

Code:
```for(i=0;i<SIZE;i++) {
for(j=i+1;j<SIZE;j++) {
if((strcmp(string1, string2)) > 0) {
}
}
}```
3) The last for loop in bold type, you should delete.

4) When a swap is made, the struct needs to be swapped, of course.

If you don't use structs, then ALL that persons array data, needs to be swapped. Not just their names.

10. Originally Posted by quzah
Why do you even have that loop you added at the end? All you need to do was to add a third set of copy lines, like the first two blocks of strcmp.
strcmp or strcpy?
You mean like:
strcpy(tmp3,punteggio[j]);
strcpy(punteggio[j],punteggio[posMin]);
strcpy(punteggio[posMin],tmp3);

Quzah.
Specifically:
1) add int i to the variables.
2) Put curly braces around both your for loops, and your if statement.

If you indent subordinate lines just 3 spaces or so, it will be MUCH clearer. It should look like this: (this is a Substitution sort)

Code:
```for(i=0;i<SIZE;i++) {
for(j=i+1;j<SIZE;j++) {
if((strcmp(string1, string2)) > 0) {
}
}
}```
3) The last for loop in bold type, you should delete.

4) When a swap is made, the struct needs to be swapped, of course.

If you don't use structs, then ALL that persons array data, needs to be swapped. Not just their names.
How can that be done? That's what I've been trying to do all the time.

11. This is what i got:
Code:
``` for ( j = 0; j < cnum -1; j++){
posMin = j;

for (i = j+1; i < cnum; i++)
if (strcmp(cognome[i], cognome[posMin]) < 0)
posMin = i;

strcpy(tmp,cognome[j]);
strcpy(cognome[j],cognome[posMin]);
strcpy(cognome[posMin],tmp);

strcpy(tmp2,nome[j]);
strcpy(nome[j],nome[posMin]);
strcpy(nome[posMin],tmp2);

tmp3=punteggio[j];
punteggio[j]=punteggio[posMin];
punteggio[posMin]=tmp3;
}```
Still not right though...

12. Code:
```char tlast[40];
char tfirst[40];
int tnum;

for ( i = 0; i < cnum -1; i++) {
for (j = i+1; j < cnum; j++) {
if ((strcmp(last[i], last[j])) > 0) {
strcpy(tlast,last[i]);
strcpy(last[i],last[j]);
strcpy(last[j],tlast);

strcpy(tfirst,first[i]);
strcpy(first[i],first[j]);
strcpy(first[j],tfirst);

tnum=number[i];
number[i]=number[j];
number[j]=tnum;
}
}
}```
That should do it.

13. Thanks for replying So it looks like that's what I was missing. Works like a charm.

14. You're welcome.