I have a CSV file from a leaderboard that looks like this:
1,coldeggman,34m38s
2,Mp16,34m43s
3,Nick_,36m03s
4,Zoekay,36m17s
5,Jonas,36m47s
6,ambaharmony,37m02s
7,ShamrockPA,37m16s
8,susslord,37m28s
9,Totalled,37m53s
10,Shape,38m32s
I have coded this struct which is used to read the file:
struct Player {
string username;
unsigned paidoff_seconds;
unsigned rank;
};
I have a function called display which whose job is to display the leaderboard. here is the code for that:
void display(const Player arr[], unsigned n) {
cout << setw(15) << "Rank" << setw(15) << "Username" <<
setw(15) << "Time" << endl;
for (int i = 0; i < n; i++) {
cout << setw(15) << arr[i].rank << setw(15) << arr[i].username << setw(15) <<
arr[i].paidoff_seconds << 's' << endl;
}
}
My question is how do I display my leaderboard so that it is in alphabetical order. I figured that in order to do this I needed to create another function who sorts through the usernames and reorders them using a bubble sort algorithm. Here is the code I created:
void sort_username(Player arr[], unsigned n) {
for (int i = 0; n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j].username > arr[j + 1].username) {
string temp = arr[j].username;
arr[j].username = arr[j + 1].username;
arr[j + 1].username = temp;
}
}
}
}
I want to then use the display function to display the newly ordered array but it is not working and I assume it is because there are other variables to take in other than username such as rank, and paidoff_seconds. any advice?