home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / ansi / stdlib / qsort.txh < prev    next >
Encoding:
Text File  |  1995-07-10  |  1.4 KB  |  72 lines

  1. @node qsort, misc
  2. @subheading Syntax
  3.  
  4. @example
  5. #include <stdlib.h>
  6.  
  7. void qsort(void *base, size_t numelem, size_t size,
  8.            int (*cmp)(const void *e1, const void *e2));
  9. @end example
  10.  
  11. @subheading Description
  12.  
  13. This function sorts the given array in place.  @var{base} is the address
  14. of the first of @var{numelem} array entries, each of size @var{size}
  15. bytes.  @code{qsort} uses the supplied function @var{cmp} to determine
  16. the sort order for any two elements by passing the address of the two
  17. elements and using the function's return address. 
  18.  
  19. The return address of the function indicates the sort order:
  20.  
  21. @table @asis
  22.  
  23. @item Negative
  24.  
  25. Element @var{e1} should come before element @var{e2} in the resulting
  26. array. 
  27.  
  28. @item Positive
  29.  
  30. Element @var{e1} should come after element @var{e2} in the resulting
  31. array. 
  32.  
  33. @item Zero
  34.  
  35. It doesn't matter which element comes first in the resulting array. 
  36.  
  37. @end table
  38.  
  39. @subheading Return Value
  40.  
  41. None.
  42.  
  43. @subheading Example
  44.  
  45. @example
  46. typedef struct @{
  47.   int size;
  48.   int sequence;
  49. @} Item;
  50.  
  51. int qsort_helper_by_size(void *e1, void *e2)
  52. @{
  53.   return ((Item *)e2)->size - ((Item *)e1)->size;
  54. @}
  55.  
  56. Item list[100];
  57.  
  58. qsort(list, 100, sizeof(Item), qsort_helper_by_size);
  59.  
  60. int qsort_stringlist(void *e1, void *e2)
  61. @{
  62.   return strcmp(*(char **)e1, *(char **)e2);
  63. @}
  64.  
  65. char *slist[10];
  66.  
  67. /* alphabetical order */
  68. qsort(slist, 10, sizeof(char *), qsort_stringlist);
  69.  
  70. @end example
  71.  
  72.