Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help using qsort with an array of structs

Now, I have seen various examples, but I don't get what they mean.

Here's my structure

typedef struct profile{
    char gender[1];
    double soc;
       . . .
} PROFILE;

where soc is social security number that I'm going to be sorting by.

I know you need a compare function, but I don't know how to come up with the exact thing I need.

like image 887
Julian Hernandez Avatar asked May 24 '11 03:05

Julian Hernandez


People also ask

How would you use qsort () function to sort an array of structures?

To sort the array, use qsort() and pass a comparison function. Notes: the simple subtraction of values produces incorrect results if the difference does not fit in the int type. For example -2 and INT_MAX cannot be correctly compared with the subtraction method.

Does qsort sort in ascending order?

qsort() — Sort ArrayThe sorted array elements are stored in ascending order, as defined by your compare function. You can sort in reverse order by reversing the sense of “greater than” and “less than” in compare. The order of the elements is unspecified when two elements compare equally.

How does qsort function work?

The qsort() function sorts an array of num elements, each of width bytes in size, where the first element of the array is pointed to by base. The compare pointer points to a function, which you supply, that compares two array elements and returns an integer value specifying their relationship.

What algorithm does qsort use?

As the name suggests, qsort function uses QuickSort algorithm to sort the given array, although the C standard does not require it to implement quicksort.


2 Answers

Here is an example of using qsort for an array of structs in C

/* qsort example */
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int price;
    int id;
} order;
order list[6];
int i = 0;

int compare (const void * a, const void * b)
{

  order *orderA = (order *)a;
  order *orderB = (order *)b;

  return ( orderB->price - orderA->price );
}

int main ()
{
    srand ( time(NULL) );

    printf("Before sorting\n");
    for(i=0; i<6; i++){ 
        list[i].price = rand()%10;
        list[i].id = i; 
        printf ("Order id = %d Price = %d \n",list[i].id, list[i].price);           
    }
    printf("AFTER sorting\n");
    int n;
    qsort (list, 6, sizeof(order), compare);
    for (n=0; n<6; n++)
         printf ("Order id = %d Price = %d \n",list[n].id, list[n].price);          
    return 0;
}

hope it helps

katerina dimitris

(all regards to pitsi)

like image 54
koko.auth Avatar answered Oct 21 '22 12:10

koko.auth


Your Soc should almost certainly not be of type double, but anyway here's an example of what a compare function needs to return:

int compare(const void *p1, const void *p2)
{
    const struct profile *elem1 = p1;    
    const struct profile *elem2 = p2;

   if (elem1->soc < elem2->soc)
      return -1;
   else if (elem1->soc > elem2->soc)
      return 1;
   else
      return 0;
}

Thanks for pointing out the const void *.

Here is a complete example (archived): Sorting Structures with the C qsort() Function

like image 34
Mitch Wheat Avatar answered Oct 21 '22 10:10

Mitch Wheat