I'm getting weird results from trying to use qsort on this array of structs.
I have this struct:
struct access_data{
int sector;
int arrival_time;
int checked;
int processed;
};
I construct an array of access_data pointers from a file such that they are sorted by arrival_time, but I need to sort them by sector later, so I have the following:
int compare_data(const void* a, const void* b){
if (((access_data*)a)->sector < ((access_data*)b)->sector)
return 1;
else if (((access_data*)a)->sector > ((access_data*)b)->sector)
return -1;
else
return 0;
}
void scan(access_data* data[], int len, int sec_to_sec_seek){
qsort(data, len, sizeof(access_data*), &compare_data);
show_data(data, len);
}
show_data simply prints the data, but I get the following on a sample input; again, sorted already by arrival time:
data[0]: arrival_time: 7, sector: 3
data[1]: arrival_time: 6, sector: 8
data[2]: arrival_time: 5, sector: 6
data[3]: arrival_time: 4, sector: 5
data[4]: arrival_time: 3, sector: 12
data[5]: arrival_time: 2, sector: 10
data[6]: arrival_time: 1, sector: 1
data[7]: arrival_time: 0, sector: 2
It is simply not sorting by sector, but by reverse arrival time. I'm really at a complete loss at to what could be causing this behavior.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…