void sortdata (competitor_data *competitors) { /* insertion sort the data items...it's more efficient than a bubble sort and not too complex */ int x = 0, y = 0; /* a temporary data item to hold the item currently being sorted */ struct competitor_data tempitem; /* variables to hold the total catch size for the 'current' data items being compared */ int this_total_weight = 0, next_total_weight = 0; for (x = 1; x < NUM_COMPETITORS; x++) { y = x; tempitem = competitors[x]; /* need to calculate the total weights * to compare, once each time the outer loop * for 'this' item, and once each time round * the loop for the 'next' item */ this_total_weight = competitors[y].river_weight + competitors[y].sea_weight + competitors[y].fly_weight; next_total_weight = competitors[y-1].river_weight + competitors[y-1].sea_weight + competitors[y-1].fly_weight; while ((y > 0) && (this_total_weight > next_total_weight)) { competitors[y] = competitors[y-1]; y--; next_total_weight = competitors[y-1].river_weight + competitors[y-1].sea_weight + competitors[y-1].fly_weight; } competitors[y] = tempitem; } }