Microsoft KB Archive/73853

= INFO: Sorting Structures with the C qsort Function =

Article ID: 73853

Article Last Modified on 12/12/2003

-

APPLIES TO

 The C Run-Time (CRT), when used with:  Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition</li></ul>

 Microsoft Visual C++ 1.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 5.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q73853

<div class="notice_section">

<div class="summary_section">

SUMMARY
The Microsoft C and QuickC run-time library function qsort is useful for sorting data; however, it is necessary to provide qsort a compare function for the type of data being sorted. This sometimes causes confusion when that type is a structure. The sample program below illustrates how to use qsort to sort an array of structures.

<div class="moreinformation_section">

MORE INFORMATION
In the sample code, the animal structure contains both an integer, which is the key to be sorted on, and an array of char that contains the animal's name. The compare function receives two pointers to type struct animal and returns -1, 1, or 0 (zero) if the first element is less than, greater than, or equal to the first element, respectively.

Sample Code
/* Compile options needed: none * * This example program uses the C run-time library function qsort * to sort an array of structures. */


 * 1) include <stdio.h>
 * 2) include <stdlib.h>

typedef int (*compfn)(const void*, const void*);

struct animal { int number; char name[15]; };

struct animal array[10] = { {  1, &quot;Anaconda&quot;    }, { 5, &quot;Elephant&quot;    }, { 8, &quot;Hummingbird&quot; }, { 4, &quot;Dalmatian&quot;   }, { 3, &quot;Canary&quot;      }, { 9, &quot;Llama&quot;       }, { 2, &quot;Buffalo&quot;     }, { 6, &quot;Flatfish&quot;    }, { 10, &quot;Zebra&quot;      }, { 7, &quot;Giraffe&quot;     }  };

void printarray(void); int compare(struct animal *, struct animal *);

void main(void) {  printf(&quot;List before sorting:\n&quot;); printarray;

qsort((void *) &array,             // Beginning address of array   10,                                 // Number of elements in array   sizeof(struct animal),              // Size of each element   (compfn)compare );                  // Pointer to compare function

printf(&quot;\nList after sorting:\n&quot;); printarray; }

int compare(struct animal *elem1, struct animal *elem2) {  if ( elem1->number < elem2->number) return -1;

else if (elem1->number > elem2->number) return 1;

else return 0; }

void printarray(void) {  int i;

for (i = 0; i < 10; i++) printf(&quot;%d: Number %d is a %s\n&quot;,               i+1, array[i].number, array[i].name); }

Keywords: kbinfo kbcrt kbcode KB73853

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.