home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_04 / 1104108b < prev    next >
Text File  |  1993-02-05  |  668b  |  39 lines

  1. /* sort2.c: Sort strings in descending order */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #define NELEMS 4
  8.  
  9. static int scomp(const void *, const void *);
  10.  
  11. main()
  12. {
  13.     size_t i;
  14.     char *some_strings[NELEMS] =
  15.       {"well","hello","you","rascal"};
  16.  
  17.     qsort(some_strings, NELEMS, sizeof some_strings[0], scomp);
  18.  
  19.     for (i = 0; i < NELEMS; ++i)
  20.         puts(some_strings[i]);
  21.     return 0;
  22. }
  23.  
  24. static int scomp(const void *p1, const void *p2)
  25. {
  26.     char *a = * (char **) p1;
  27.     char *b = * (char **) p2;
  28.  
  29.     /* Negate for descending order */
  30.     return -strcmp(a,b);
  31. }
  32.  
  33. /* Output:
  34. you
  35. well
  36. rascal
  37. hello
  38. */
  39.