Sorting Structures
    Gary Smith 
    nsfacgs at prism.gatech.EDU
       
    Sun Jun  2 07:44:27 AEST 1991
    
    
  
Can someone explain why this sort will not work under BC++. Thanks.
------Snip--------
#include    <stdio.h>
#include    <stdlib.h>
#include    <string.h>
struct personel
{
    char name[30];
    int age;
} employee[5];
int my_sort(struct personel *s, struct personel *d);
main()
{
int     x;
    strcpy(employee[0].name, "John Doe");
    employee[0].age = 30;
    strcpy(employee[1].name, "Jane Doe");
    employee[1].age = 28;
    strcpy(employee[2].name, "Joanne Doe");
    employee[2].age = 5;
    strcpy(employee[3].name, "Jimmy Doe");
    employee[3].age = 12;
    strcpy(employee[4].name, "Josh Doe");
    employee[4].age = 8;
    for ( x=0; x<5; x++ )
        printf("NAME: %s AGE: %d\n", employee[x].name, employee[x].age);
    printf("\n\n");
    qsort(employee, 5, sizeof (struct personel), my_sort);
    for ( x=0; x<5; x++ )
        printf("NAME: %s AGE: %d\n", employee[x].name, employee[x].age);
    return(0);
}
/* Normally this function will return the opposite (-1 or 1) but since
   we want this to be in DECENDING order, you have to do it this way */
int my_sort(struct personel *s, struct personel *d)
{
    if ( s->age < d->age )
        return(1);
    if ( s->age > d->age )
        return(-1);
    return(0);
}
-------End Snip--------
    
    
More information about the Comp.lang.c
mailing list