0
struct players {
      string team_name;                    
      string driver;   
      string navigator;                  
      double best_time; } world[32];

I am generating random values for the best time and using a bubble sort in ascending order for the best time. See below. How do I sort for best time but retain my team name, driver and navigator?

------------------------------------------

double temp;
int a, b;

for (a=0; a < 32;a++)
{                

    for (b=0; b<32-a-1; b++)
        if (world[b].best_time < world[b+1].best_time)
           {
           temp = world[b].best_time;
           world[b].best_time = world[b+1].best_time;
           world[b+1].best_time = temp;
           }
2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by tnimblett
0

Swap the entire structure, not just the time

struct players temp;
int a, b;

for (a=0; a < 32;a++)
{                

    for (b=0; b<32-a-1; b++)
        if (world[b].best_time < world[b+1].best_time)
           {
           temp = world[b];
           world[b] = world[b+1];
           world[b+1] = temp;
           }
0

One update for future seekers. You have to declare the temporary value as a struct as well.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.