title: Organizing a struct in alphabetical order with qsort
body: <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  #define ARRAY_MAX 100
  #define FNAME 31
  #define LNAME 31
  
  typedef int (*compfn)(const void*, const void*);
  
  struct student {
  
      char firstName[FNAME];
      char lastName[LNAME];
      int quiz1, quiz2 , quiz3, quiz4, mid1, mid2, final;
      char letterGrade;
      int finalGradePercentage;
  };
  
  // Function for Qsort
  int compare(struct student *element1, struct student *element2)
  {
      if ( element1-&gt;firstName[0] &lt; element2-&gt;firstName[0])
          return -1;
  
      else if (element1-&gt;firstName[0] &gt; element2-&gt;firstName[0])
          return 1;
  
      else
          return 0;
      }
  
  
  int main(int argc, char *argv[]){
  
  
      FILE *inputFile, *outputFile;
      char buf[100];
      char *token;
      int count = 0;
  
      inputFile = fopen("/Users/Home/Desktop/input_data.txt", "r");
  
  
      struct student studentData[ARRAY_MAX];
      while (fgets(buf, sizeof(buf), inputFile) != NULL){ //scan through the input file
          token = strtok(buf, " ");
          strcpy(studentData[count].firstName, token);
          token = strtok(NULL, ",");
          strcpy(studentData[count].lastName, token);
          token = strtok(NULL, ",");
          studentData[count].quiz1 = atoi(token);
          token = strtok(NULL, ",");
          studentData[count].quiz2 = atoi(token);
          token = strtok(NULL, ",");
          studentData[count].quiz3 = atoi(token);
          token = strtok(NULL, ",");
          studentData[count].quiz4 = atoi(token);
          token = strtok(NULL, ",");
          studentData[count].mid1 = atoi(token);
          token = strtok(NULL, ",");
          studentData[count].mid2 = atoi(token);
          token = strtok(NULL, ",");
          studentData[count].final= atoi(token);
  
  
          studentData[count].finalGradePercentage = studentData[count].quiz1 * .10 +studentData[count].quiz2 * .10 + studentData[count].quiz3 * .10 + studentData[count].quiz4 * .10 + studentData[count].mid1 * .20 + studentData[count].mid2 * .15 + studentData[count].final * .25;
  
          if (studentData[count].finalGradePercentage &lt;=100 &amp;&amp;  studentData[count].finalGradePercentage&gt;=90 )
          {
              studentData[count].letterGrade = 'A';
          }
          else if(studentData[count].finalGradePercentage &lt;= 89 &amp;&amp; studentData[count].finalGradePercentage &gt;=80)
          {
              studentData[count].letterGrade = 'B';
          }
          else if(studentData[count].finalGradePercentage &lt;= 79 &amp;&amp; studentData[count].finalGradePercentage &gt;=70)
          {
              studentData[count].letterGrade = 'C';
          }
          else if(studentData[count].finalGradePercentage &lt;= 69 &amp;&amp; studentData[count].finalGradePercentage &gt;=60)
          {
              studentData[count].letterGrade = 'D';
          }
          else if(studentData[count].finalGradePercentage &lt;= 59){
              studentData[count].letterGrade = 'F';
          }
  
  
          count++;
      }
  
  
      qsort((void *) &amp;studentData,count,sizeof(struct student),(compfn)compare);           
  
  
      for (int i = 0; i&lt; count; i++) {
          char name[62];
  
          sprintf(name, "%s %s", studentData[i].firstName, studentData[i].lastName);
          printf("%-20s %c\n", name, studentData[i].letterGrade);
  
      }
  
      fclose(inputFile);
      fclose(outputFile);
  
      return 0;
  }
  </code></pre>
  
  <p>Hi Guys 
  I'm having a tough time getting my struct organized in alphabetical order. Any help doing so would be greatly appreciated. This is what I have so far. I think the problem is the in the compare function. But I'm not sure I really understand qsort all that well. Anyone one know of some good references for reading up on it.  </p>