Buffalo282
Buffalo282

Reputation: 29

Sorting a String Array Alphabetically C++

I'm trying to write a program that is given the following structures:

struct aPlayer {
  string name;  // name of player
  int wins;     // number of wins player has
};

struct aCompetition {
  string  name;                 // name of the match
  int     numPlayers;           // number of players in the club
  aPlayer player[10];           // list of players in this club
};

From there I want to write a function that will sort the players by name alphabetically. The function declaration would be as follows:

    void sortByName(aCompetition & c){}

Note: I would like to do this by only using for loops, while loops, and if statement(s). The only way I could think to compare the two strings would be to compare their ASCII values. I'm not sure how to do that so any input will be greatly appreciated. Thanks!

Upvotes: 1

Views: 40690

Answers (3)

Jish
Jish

Reputation: 92

Assuming this is for homework (and if it's not, doing this by yourself will help you a lot more than just seeing the answer,) I'm just going to give you a few pointers to help you out.

Compare ASCII values:

aPlayer player1, player2;
player1.name = "bill";
player2.name = "john";
if (player1.name[0] < player2.name[0])
{
    // True, in this case, because b is less than j on the ascii table.
}

http://www.asciitable.com for the ascii values. I recommend using tolower() on the player names, because capital letters are lower values than lower case letters.

If the first digit is equal, move on to the second: (One way of doing this.)

aPlayer player1, player2;
player1.name = "alfred";
player2.name = "alvin";

// Find which name is shorter using .length() like player2.name.length()

// Loop through this next part for all aPlayers in aCompetition
for (int i = 0; i < shorterName.length(); i++)
{
    // Compare ascii values as I showed above.
    // If one is larger than the other, swap them.
}

Upvotes: 1

ephemient
ephemient

Reputation: 204758

Sorting is provided by the standard library, on types with an operator<, or other types if given that comparator. You can build one off of string::operator< which performs lexical comparison.

#include <algorithm>
void sortByName(aCompetition& c) {
    sort(&c.player[0], &c.player[c.numPlayers],
            [](const aPlayer& a, const aPlayer& b) {return a.name < b.name;});
}

If you don't have C++11 lambdas then you'd use a functor.

struct compareAPlayerByName {
    boolean operator()(const aPlayer& a, const aPlayer& b) {
        return a.name < b.name;
    }
};
void sortByName(aCompetition& c) {
    sort(&c.player[0], &c.player[c.numPlayers], compareAPlayerByName());
}

Upvotes: 3

Eric Adam Fortney
Eric Adam Fortney

Reputation: 82

A simple solution for doing this is storing the values as a set. This is a fairly standard way to store data in C++ and has the advantage of automatically sorting alphanumerically. You will have to wrap your head around iterators though to output them effectively.

Consider this execution:

std::set sortByNames(aCompetition & c, int numPlayers)
{
   std::set<std::string> sortedNames;

   for(int i = 0; i < numPlayers; i++)
   {
       std::string name;
       //std::cout << i << ". ";
       name = player[i];

       sortedNames.insert(name);
   }
   return sortedNames;
}

From here you can use this to output the names:

myNames = sortByNames(aCompetition, 10);
std::for_each(myNames.begin(), myNames.end(), &print);

You will also need an #include <set> in your header file.

Upvotes: 0

Related Questions