If you store your different player structs in an STL container such as a list or a vector you can then use the sort function on that container and automatically sort them based on whatever criteria you want. For instance to sort in ascending order by score_avg you could do:
Code:
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std; // Edit: Forgot this
// This is a simplified struct just to illustrate my point...
struct team
{
float score_avg; // Average Points player makes per game
};
// Define a function to compare the score_avg data members of two
// different team structures. You will need to create two additional
// comparison functions to compare based on the name and the
// grade point average.
bool score_comp( const team& t1, const team& t2)
{
return t1.score_avg < t2.score_avg;
}
int main()
{
vector<team> players; // Hold a whole mess of team structures
vector<team>::iterator it;
team player; // Hold a single players data
// Add some team structs to the vector. You will need to replace
// this with your code to open your file and read all the data into
// your team struct and push it into the vector.
player.score_avg = 54.8;
players.push_back(player); // Players contains 1 team struct w/ value 54.8
player.score_avg = 47.2;
players.push_back(player); // Players contains 2 team structs w/ values 54.8, 47.2
player.score_avg = 99.8;
players.push_back(player); // Players contains 3 team structs w/ values 54.8, 47.2, 99.8
// Now display the unsorted vector...
// This will output 54.8, 47.2, 99.8 on individual lines
for( it = players.begin(); it != players.end(); ++it )
cout << it->score_avg << endl;
// Now sort the vector using the score_comp function
// Replace score_comp with name of your other comparison
// functions to sort the data in players vector differently
// based on your needs.
sort(players.begin(),players.end(),score_comp);
// Now display the vector again, should be sorted...
// This will output 47.2, 54.8, 99.8 on individual lines
for( it = players.begin(); it != players.end(); ++it )
cout << it->score_avg << endl;
return 0;
}
This will give you an idea of how easy it can be to sort data once it is in a vector. You can sort in reverse order based on the score_avg data member just by changing the '<' to '>' in the score_comp function.