I know there are similiar posts to this one but I can't get my head around them so I'm making one too so please don't mark this as a duplicate :)
I've got 2 nested arrays of structs where the outer structs are races and the inner structs are the number of boats from those races. I want to sort the inner struct so that the boats are in ascending order of time_to_complete_race and once I've done this I then assign the first boat in the array points = 4, the second points = 2 and the thirdpoints = 1. I haven't however implemented the points assinging yet so please ignore this part.
struct boat_data {
int ID;
int time_to_complete_race;
int points;
} boat_node;
typedef struct race_result {
char race_date[80];
int start_time;
int num_boats_competing;
struct boat_data boat_data[MAX_BOAT_NUMBER];
} race_node;
Here is the code that I am using to sort the inner stucts:
void print_races(int races, race_ptr results[], member_node_ptr member) {
for(int race = 0; race < races; race++) {
printf("Race Date: %s\n", results[race].race_date);
printf("Race Start Time: %d\n", results[race].start_time);
printf("Number of Skippers: %d\n", results[race].num_boats_competing);
for(int boats = 0; boats < results[race].num_boats_competing; boats++) {
find_node_by_id(member, results[race].boat_data[boats].ID);
printf("\tTime to Complete Race in Seconds: %d\n",
results[race].boat_data[boats].time_to_complete_race);
}
printf("\n");
}
}
void print_sorted_races(int races, race_ptr results[], member_node_ptr member) {
race_ptr sorted_results[races];
struct boat_data temp;
int race, swap, boat;
for (race = 0; race < races; race++) {
sorted_results[race] = results[race];
}
for (race = 0; race < races; race++) {
for (boat = 0; boat< (sorted_results[race].num_boats_competing -1); boat++) {
for (swap = race + 1; swap < sorted_results[race].num_boats_competing; swap++) {
if (sorted_results[race].boat_data[swap].time_to_complete_race >
sorted_results[race].boat_data[boat].time_to_complete_race) {
temp = sorted_results[race].boat_data[boat];
sorted_results[race].boat_data[boat] = sorted_results[race].boat_data[swap];
sorted_results[race].boat_data[swap] = temp;
}
}
}
}
print_races(races, results, member);
}