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;
           }

Recommended Answers

All 2 Replies

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;
           }

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.