delorie.com/djgpp/doc/libc/libc_635.html   search  
libc.a reference

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

qsort

Syntax

 
#include <stdlib.h>

void qsort(void *base, size_t numelem, size_t size,
           int (*cmp)(const void *e1, const void *e2));

Description

This function sorts the given array in place. base is the address of the first of numelem array entries, each of size size bytes. qsort uses the supplied function cmp to determine the sort order for any two elements by passing the address of the two elements and using the function's return address.

The return address of the function indicates the sort order:

Negative

Element e1 should come before element e2 in the resulting array.

Positive

Element e1 should come after element e2 in the resulting array.

Zero

It doesn't matter which element comes first in the resulting array.

Return Value

None.

Portability

ANSI/ISO C C89; C99
POSIX 1003.2-1992; 1003.1-2001

Example

 
typedef struct {
  int size;
  int sequence;
} Item;

int qsort_helper_by_size(const void *e1, const void *e2)
{
  return ((const Item *)e2)->size - ((const Item *)e1)->size;
}

Item list[100];

qsort(list, 100, sizeof(Item), qsort_helper_by_size);

int qsort_stringlist(const void *e1, const void *e2)
{
  return strcmp(*(char **)e1, *(char **)e2);
}

char *slist[10];

/* alphabetical order */
qsort(slist, 10, sizeof(char *), qsort_stringlist);


  webmaster     delorie software   privacy  
  Copyright © 2004     Updated Apr 2004