1

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!

3
  • std::string supports less-than and greater-than comparisons. I'd use std::sort, but if you are restricted to what you can use a simple bubble sort would be fine and you can easily find that algorithm. Commented Apr 7, 2017 at 2:38
  • Should aCompetition really be a struct? Considering it holds an array? Commented Apr 7, 2017 at 5:13
  • Seems perfectly normal to me. A competition contains players. Commented Apr 7, 2017 at 5:15

3 Answers 3

3

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());
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.