Your code had an index array in it, why not sort with that?
Code:
/*
Daniel Sedin VAN L 73 38
Steven Stamkos TBL C 72 43
Henrik Sedin VAN C 73 18
Martin StLouis TBL R 72 26
Corey Perry ANA R 72 39
Alex Ovechkin WSH L 73 29
Henrik Zetterberg DET L 73 21
Jonathan Toews CHI C 70 30
Anze Kopitar LAK C 73 25
Jarome Iginla CGY R 75 33
Patrick Sharp CHI L 71 34
Brad Richards DAL C 62 25
Teemu Selanne ANA R 63 22
Sidney Crosby PIT C 41 32
*/
#include <stdio.h>
#include <string.h>
#define MAX 14
void sort(int index[MAX], char fname[][22], int key);
int main(void) {
int i = 0, j, got, len, index[MAX];
char fname[MAX][22];
char lname[MAX][22];
char team[MAX][4];
char plays[MAX];
int data[MAX][2];
FILE *fp;
printf("\n\n");
if((fp=fopen("team.dat","r")) == NULL) {
printf("Error opening data file\n");
return 0;
}
while(1) { //giving fscanf() it's own line
got = fscanf(fp,"%s %s %s %c %d %d", fname[i],lname[i],team[i],&plays[i],&data[i][0],&data[i][1]);
if(got < 6)
break;
index[i] = i; //you're building an index array at the same time? Good!
++i; //welcome to C! :) = i + 1;
}
//i has just counted the number of names in the array, so it can be used:
printf("\n\n");
for (j = 0; j < i; j++) { //start with 0 not 1
printf("%s %s %s %c %d %d\n",fname[j],lname[j],team[j],plays[j],data[j][0],data[j][1]);
}
sort(index, fname,1);
printf("\n\n");
for (j = 0; j < MAX; j++) { //start with 0
i = index[j];
printf("%11s %-11s %4s %c %2d %2d\n",fname[i],lname[i],team[i],plays[i],data[i][0],data[i][1]);
}
fclose(fp);
printf("\n\n\t\t\t press enter when ready");
(void) getchar();
return 0;
}
void sort(int index[MAX], char fname[][22], int key) {
int i, j, temp;
if(key==1) { //sort by first name
/* This is substitution sort - similar to
bubble sort, but slightly faster
*/
for(i=0;i<MAX-1;i++) {
for(j=i+1;j<MAX;j++) {
/*compare strings i with j, using the index numbers
no actual data is swapped out, with this index sort
*/
if(strcmp(fname[index[i]], fname[index[j]]) > 0) {
temp = index[i];
index[i] = index[j];
index[j] = temp;
}
}
}
}
else if(key == 2) {
//etc.
}
}