[Solved] how to sort array of c++ strings alphabetically [closed]


It seems to me that your algorithm is wrong. Try making a few examples and run them by hand.
Try this code instead:

#include <algorithm>

bool comp(string a, string b)
{
    return a[0] < b[0];
}

void sort(player *player_array, num_players)
{
    string sorted[num_players];
    for (int i = 0; i < num_players; ++i)
        sorted[i] = player_array[i].name;
    std::sort(sorted, sorted + num_players, comp);
}

This solution uses std::sort so complexity is O(n log n). But since you are only sorting the names by their first character, it can be optimized down to O(n) using counting sort.

solved how to sort array of c++ strings alphabetically [closed]