Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sorting a struct in C w/ qsort

    How would i go about sorting an array of this struct in C w/ qsort???
    typedef struct{
    int hour,min;
    } Time;
    typedef enum {Sun,Mon,Tue,Wed,Thu,Fri,Sat} DoWeek;
    typedef struct {
    char Name[29],Band[29],Venue[29];
    DoWeek DoWeekday;
    Time start;
    int hall_size;
    float cost;
    } concert_record;

    i need to sort this database by day and by venue(in alphabetical order)

  • #2
    New to the CF scene
    Join Date
    Nov 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oopps i was having troubles but i figured it out here is my sample if anyone wants to know how to do this...

    #include<stdio.h>
    #include<stdlib.h>
    int compfunc_int(const void *x, const void *y);

    typedef struct {
    int id;
    char name[15];
    }A;

    main(){
    int i;
    A data[3]={ {12,"Jason/0"},{48,"Nick/0"},{10,"Dave/0"}};
    for(i=0;i<3;i++)
    printf("%d",data[i].id);
    printf("\n");
    qsort(data,3,sizeof(A),compfunc_int);
    for(i=0;i<3;i++){
    printf("%d",data[i].id);
    }
    printf("\n");
    return 0;
    }
    /* this will work for day as it is numerical*/
    int compfunc_int(const void *x, const void *y)
    { return ((A *)x)->id -
    ((A *)y)->id;
    /*return (int) (*x -*y);*/
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •