...

View Full Version : Sorting a struct in C w/ qsort



diesavagenation
11-23-2004, 02:56 PM
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)

diesavagenation
11-23-2004, 03:29 PM
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);*/
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum