title: Alphabetizing structured lists by last name in C
body: <p>I am attempting to sort a structure by last names alphabetically.  I have got most of my code to work correctly, but I have not been able to get it to sort alphabetically.  I attempted to use a swapper method, where you have a temp holder, swap the temp with the [s], and swap the [s] with the [s+1], but I could not get it to swap correctly.  I attempted to make a function and transfer the structure to the function, but I could not get that to function correctly either.  I am new to C and my syntax isn't that great.  I have gotten decent at sorting arrays full of numbers, but I haven't been able to find any examples of sorting structures to go by.  Any help would be greatly appreciated.  Here is my code so far: </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct student{
  char f_name[20];
  char l_name[20];
  char temp[20];
  int id_num;
  float test1;
  float test2;
  float test3;
  }s[10];
  
  int main(void)
  {
  char grade;
  float average;
  int i, j, b;
  
  for(i=0;i&lt;10;i++)
  {
  printf("Enter first name(ZZ to quit): ");
  scanf("%s", &amp;s[i].f_name);
  if(strcmp(s[i].f_name, "ZZ")==0)
      break;
  else
  {
  printf("Enter students last name: ");
  scanf("%s", &amp;s[i].l_name);
  printf("Enter Student ID: ");
  scanf("%d", s[i].id_num);
  printf("Enter 3 test scores: ");
  scanf("%f %f %f", &amp;s[i].test1, &amp;s[i].test2, &amp;s[i].test3);
      b++;
      }
  }
  
  i--;
  printf("Student\t\tStudent Id\tFinal Grade\n");
  for(i;i&gt;=0;i--)
  {
  average = (s[i].test1 + s[i].test2 + s[i].test3) / 3;
  if(average &gt;= 90)
     grade = 'A';
  else if(average &gt;=80 || &lt; 90)
     grade = 'B';
  else if(average &gt;= 70 || &lt; 80)
     grade = 'C';
  else if(average &gt;= 60 || &lt; 70)
     grade = 'D';
  else
     grade = 'F';
  
  printf("%s, %s\t%d\t\t%.2f \t%c\n", s[i].l_name, s[i].f_name, s[i].id_num, average, grade);
  }
  return 0;
  }
  </code></pre>