How to sort boy and girl names? :)

Hi there! I'm confused on how to sort boy and girl names. Like, in a country where I live, all of the boy names end with a 'as' or 'us' characters, and girl names end with a 'a' or 'e' characters. Here's a code where I find 10 most popular names in school. SO how to sort those 10 names?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for ( string name; in >> name; ) M[name]++;


        for ( int i = 1; i <= 10 && !M.empty(); i++ )
        {
            auto it = max_element( M.begin(), M.end(), []( PR a, PR b )
            {
                return a.second < b.second || ( a.second == b.second && a.first > b.first );
            } );

            cout << i << ". " << (*it).first << ' ' << (*it).second << '\n';
            fv << "   " << i << ". " << (*it).first << endl;

            M.erase(it);
        }
Last edited on
you need to write your own comparison function for std::sort.
I would just check whether the last char is 's' for simplicity.
if you want them in order on top of being sorted boy/girl, you need to make it more complex, but only a little; if the input is boy/girl or girl/boy you sort off the last letter else sort normally with string compare...
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;


string tolower( string str )
{
   for ( char &c : str ) c = tolower( c );
   return str;
}


void write( const string &title, const vector<string> &V )
{
   cout << title << '\n';
   for ( auto e : V ) cout << e << '\n';
   cout << '\n';
}


int main()
{
   vector<string> boys, girls, unknown;
   stringstream in( "Brontosaurus Fenestra Iambus Confused Calamitous Ericaceous Christmas "
                    "Happygoluckas Delila Juicia Aspidistra Perambulate" );

   for ( string name; in >> name; )
   {
      string temp = tolower( name );
      reverse( temp.begin(), temp.end() );
      if       ( temp[0] == 'a'              || temp[0] == 'e'              ) girls.push_back( name );
      else if  ( temp.substr( 0, 2 ) == "su" || temp.substr( 0, 2 ) == "sa" ) boys.push_back( name );
      else                                                                    unknown.push_back( name );
   }
   sort( girls.begin(), girls.end() );
   sort( boys.begin(), boys.end() );
   sort( unknown.begin(), unknown.end() );

   write( "Girls:", girls );
   write( "Boys:", boys );
   write( "Unknown:", unknown );
}


Girls:
Aspidistra
Delila
Fenestra
Juicia
Perambulate

Boys:
Brontosaurus
Calamitous
Christmas
Ericaceous
Happygoluckas
Iambus

Unknown:
Confused
Last edited on
What is the desired order?

* First all girls in lexicographical order and then all boys in lexicographical order?
Jane
Julia
Janus
Julius
* Something else?

In the std::sort we ask "Is it ok to have A before B?"

If gender is different, then that answers the question.
Only if gender is same, then name determines the answer.
Thanks for help! :)
Topic archived. No new replies allowed.