Thread: qsort and structures

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    12

    qsort and structures

    If I have a structure with two different datatypes as fields of the structure, ie:
    Code:
    typedef struct {
    int value;
    char *name;
    } item;
    Now I have an array of these items and I want to sort them using qsort first by value, then by name. (ie, if there are two elements that have the same value, I would then sort those two elements by their name)

    How would I approach this using qsort? Thanks.

  2. #2
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    Hi!

    start by writing a function that does the comparison:

    Code:
    int mycomp(const void *p1, const void *p2)
    {
       const item *item1=p1, *item2=p2;
       int result;
    
       /* set result to: */
       /*  0 - if item1 and item2 are equal */
       /* <0 - if item1 should come before item2 */
       /* >0 - if item1 should come after item2 */
    
       return result;   
    }
    hope this helps

    alex

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    12
    Is there a way to use that function on two different fields (with same data type, ie: int)? I don't want to rewrite a function that looks almost exactly the same for each field that I have.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I think you can't use qsort() easily in this case.
    Of course you can, dead easy

    *borrows Alex's code*
    Code:
    int mycomp(const void *p1, const void *p2) {
       const item *item1=p1, *item2=p2;
       if ( item1->value < item2->value ) {
          return -1;
       } else
       if ( item1->value > item2->value ) {
          return +1;
       } else {
          return strcmp( item1->name, item2->name );
       }
    }
    
    // blah
    item array[10];
    qsort( array, 10, sizeof(item), mycomp );

  5. #5
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    theweirdo: no, you can't write one function to handle multiple fields, at least there is no neat way to do it. If you really want you can use a hack: a global variable that gives the offset of the field relative to the start of the struct.

    vVv: I think using qsort is easier than your solution. Also: mystrcmp("test", "testing") = 0.

    alex

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    4

    no global variables

    first off, global variables are a bad idea. Only use them sparenly.

    The best solution would be to use a class but c doesn't support them. This might be one of those times you might want to look at c++ instead of c.

    If you want to use a class, make the class called qsort and define qsort as having multiple return types for a member of the class.

    This would be the best solution.

    on second thought.

    try using a union as the return type for function qsort()(c style)
    Last edited by wildman6801; 02-13-2002 at 11:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-23-2008, 04:22 AM
  2. sorting union of structures with q sort
    By colw in forum C Programming
    Replies: 6
    Last Post: 04-09-2002, 09:51 AM
  3. Need help with qsort() and union of structures
    By DanTheMan in forum C Programming
    Replies: 1
    Last Post: 04-02-2002, 08:30 AM
  4. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM
  5. Using malloc() & qsort with a union
    By Cyber Kitten in forum C Programming
    Replies: 2
    Last Post: 09-02-2001, 07:57 AM