Does anyone know why this sort is not sorting the first element in the struct array? Below is the code for the struct, the sort and some sample input output. I have excluded the code that filled the array because I know from printing the array before sorting it,that it is filled correctly.
struct Record
{
char *firstName;
char *lastName;
char *GPA;
unsigned int ID;
};
void newList(struct Record * Records, int amount, char* name)
{
int i;
int j;
struct Record tempR;
FILE *fp;
unsigned int temp;
//Order Array
for(i=0;i<amount;i++)
{
for(j=0;j<amount-1;j++)
{
if(strcmp(Records[j].firstName,Records[j+1].firstName)<0)
{
tempR=Records[j];
Records[j]=Records[j+1];
Records[j+1]=tempR;
}
}
}
//Make New Fle with Ordered Array
fp=fopen(name, "w+");
for(i=0;i<amount;i++)
{
fprintf(fp,"%s, %s, %s, %d, Records[i].firstName,Records[i].lastName,
Records[i].GPA,Records[i].ID);
}
}
A sample input and output of this code is the following Input:
Yblwtjbvtz,Eoztbzoqnz,2.6,1123268861
Blmhwgzjdd,Ojwfnlislc,3.1,1712113924
Gkmkbnotic,Mhzcakkugv,3.3,1966045151
Zsrwqdwkfo,Nciqixcamr,2.1,212426241
Vrekafrafk,Ixylzenhlc,2.2,297694159
Kzkaxpoeqg,Syawkuqbew,3.4,104209687
Ekdcfsifrw,Apvrwfshqm,1.4,799470314
Iybmcotvpf,Eqvcorjntu,0.6,1748600414
Jsfwiydnyt,Rhyaabwfdr,2.2,104800253
Mfqrukoytp,Urjsjcloau,3.8,1240702350"
Output:
Yblwtjbvtz, Eoztbzoqnz, 2.6, 1123268861
Zsrwqdwkfo, Nciqixcamr, 2.1, 212426241
Vrekafrafk, Ixylzenhlc, 2.2, 297694159
Mfqrukoytp, Urjsjcloau, 3.8, 1240702350
Kzkaxpoeqg, Syawkuqbew, 3.4, 104209687
Jsfwiydnyt, Rhyaabwfdr, 2.2, 104800253
Iybmcotvpf, Eqvcorjntu, 0.6, 1748600414
Gkmkbnotic, Mhzcakkugv, 3.3, 1966045151
Ekdcfsifrw, Apvrwfshqm, 1.4, 799470314
Blmhwgzjdd, Ojwfnlislc, 3.1, 1712113924