-2

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?

Anna Topp
  • 7
  • 1
  • Does this answer your question? [C++ Sorting Class Array](https://stackoverflow.com/questions/12823573/c-sorting-class-array) – scohe001 Jan 29 '21 at 21:58
  • 1
    In what way is it not working? Wrong output? Program crash? – user4581301 Jan 29 '21 at 21:58
  • Consider manufacturing a [mre]. MRE is a powerful debugging technique, so usually you get part way into making the MRE and the reduced noise makes the problem obvious. If not, a question with a good MRE is either quickly solved by the denizens of Stack Overflow or a total bad ass that requires a domain expert and is worth preserving to help future askers. – user4581301 Jan 29 '21 at 22:09

2 Answers2

3

You might use an STL container std::vector<Player> arr instead of a C-array Player arr[] and sort on username like this:

std::sort(arr.begin(), arr.end(), [](const Player &p1, const Player &p2){ return p1.username < p2.username; });
vanadium
  • 44
  • 2
  • 2
    Right answer. Probably no use to the Asker, but the right answer all the same. – user4581301 Jan 29 '21 at 21:59
  • Note that this can also be performed on the array: `std::sort(arr, arr+n, [](const Player &p1, const Player &p2){ return p1.username < p2.username; })` – user4581301 Jan 29 '21 at 22:01
0

There are multiple errors.

Right now you are sorting on the username value. Weren't you supposed to sort on the rank?

Also you are not rearranging the Player object, you are currently only swapping the usernames.

I also would suggest writing a new display method which takes only a Player object as argument instead of the entire array and an index.

display(const Player &player) {}

gray
  • 1
  • 1
  • 1