title: Unable to sort dates with struct
body: <p>I've attempted to make a program which sorts a list of dates in the format of (month day year with year being the last two digits of the year. My program should sort the dates for years between 90 to 99 and then from 00 to 12.I can't get it to sort properly.</p>
  
  <p>So for example if the input was this:</p>
  
  <blockquote>
    <p>January 1 01</p>
    
    <p>June 30 90 </p>
    
    <p>October 1 01</p>
  </blockquote>
  
  <p>It should sort it to </p>
  
  <blockquote>
    <p>October 1 01</p>
    
    <p>January 1 01</p>
    
    <p>June 30 90 </p>
  </blockquote>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  /* constants for max chars, max day, max year, max size */
  enum { MAXC = 12, MAX_DAY = 31, MAX_YEAR = 12, MAX_SIZE = 1000 };
  
  typedef struct {
      char month[MAXC];   /* either make static or allocate separately */
      unsigned day;
      unsigned year;
  } date;
  
  /* empty character remaining in stdin */
  void empty_stdin ()
  {
      int c;
      while ((c = getchar ()) != '\n' &amp;&amp; c != EOF) {}
  }
  
  /* sort struct date on year */
  int sort (const void *a, const void *b)
  {
      date *date1 = (date *) a;
      date *date2 = (date *) b;
  
      if (date2-&gt;year != date1-&gt;year)
          return (date1-&gt;year &gt; date2-&gt;year) - (date1-&gt;year &lt; date2-&gt;year);
  
  
  
  
  
  
      return 0;
  }
  
  /* output n elements of array of struct date */
  void output (date *ar, int n)
  {
      int i;
  
      printf ("\nOutput sorted by year:\n\n");
  
      for (i = 0; i &lt; n; i++)
          printf ("  %s %d %d\n", ar[i].month, ar[i].day, ar[i].year);
  }
  
  int main (void) {
  
      int i, n;
      date *ar = NULL;
  
      while (1) {     /* obtain valid 'n', compare with using fgets below */
  
          int rtn; /* varaible to save return of scanf -- always validate */
  
          //printf ("Enter number of dates to be entered (between 1 &amp; 1000): ");
          if ((rtn = scanf ("%d", &amp;n)) != 1) {   /* if conversion failed */
              if (rtn == EOF) {   /* test for user cancelation of input */
                  fprintf (stderr, "note: user canceled input, exiting.\n");
                  return 0;
              }                   /* otherwise simply an invalid input */
              fprintf (stderr, "error: invalid input.\n");
              goto tryagain;
          }
  
          if (n &lt; 0) {            /* invalid input &lt; 0 */
              fprintf (stderr, "error: invalid input (n &lt; 0).\n");
              goto tryagain;
          }
  
          if (n &gt; MAX_SIZE) {     /* invalid input &gt; MAX_SIZE */
              fprintf (stderr, "error: invalid input (n &gt; %d).\n", MAX_SIZE);
              goto tryagain;
          }
  
          break;      /* if we are here - we have a good value, break */
  
        tryagain:;    /* label for goto to jump over break */
  
          empty_stdin ();   /* empty characters that remain in input buffer */
      }
  
      empty_stdin ();     /* empty characters that remain in input buffer */
  
      /* allocate array of struct ar, n elements */
      if ((ar = malloc (sizeof *ar * n)) == NULL) {
          fprintf (stderr, "error: virtual memory exhausted.\n");
          return 1;
      }
  
      /* provide format instructions */
      //printf ("Enter the date (month day year)\n"
             // "  format, e.g.:  Jan 18 2017\n\n");
  
      for (i = 0; i &lt; n;) {   /* loop until all elements filled */
  
          char buf[MAX_DAY + 1] = "", ans[MAXC] = "";
  
          //printf (" date[%2d] : ", i + 1);    /* prompt for input */
  
          /* if fgets return is NULL, EOF encountered */
          if (fgets (buf, MAX_DAY + 1, stdin) == NULL) {
              fprintf (stderr, "note: user canceled input, exiting.\n");
              return 0;
          }
  
          if (*buf == '\n') { /* if first char is '\n', user just hit enter */
              printf ("no input provided, quit (y/n)? ");
              if (fgets (ans, MAXC, stdin) &amp;&amp; (*ans == 'y' || *ans == 'Y'))
                  return 0;
              else if (!*ans) {   /* if ans NULL, EOF encountered */
                  fprintf (stderr, "note: user canceled input, exiting.\n");
                  return 0;
              }
          }
  
          /* parse with sscanf, validate 3 conversion took place */
          if (sscanf (buf, "%11s %u %u", ar[i].month, &amp;ar[i].day, &amp;ar[i].year) != 3)
          {
              fprintf (stderr, "error: invalid input.\n");
              continue;
          }
  
          i++;    /* only increment if valid sscanf conversion took place */
      }
  
      qsort (ar, n, sizeof (date), sort);     /* sort by year */
  
      output (ar, n);     /* output results */
  
      free (ar);      /* free ar - you allocate it, you free it */
  
      return 0;
  }
  </code></pre>