I am totally stuck. I created a program to read in the file and one that will sort from a string array. The problem is that it sorts from the beginning of each line. I need it to sort after the -. I need it to sort by the town not the name. How do I do this? Everything works up to this point.

#include <iostream>
#include <string>

using namespace std;
void sort(string ar[],int n){
}
int main()
{
int i;

  string town[] = { "",
        "Joshua Lynn Odom -  Bates Town",
        "Cindy Blue - Broken Arrow",
        "Donnie Kay - Oklahoma City",
        "Billy Bob - Durant",
        "Randall John - Lawton",
        "Tammy Smiley - Ada",
        "Danny Steven Ray - George Town",
        "Rodney White - Lone Grove",
       };

    int N = sizeof(town)/sizeof(town[0]);

    sort(town,town+N);

    cout << "SORTED TOWNS" << endl;
    for (i=0; i < N; i++)
    cout << town[i] << " " << endl;
    cout << endl << endl;
}

I created a program to read in the file and one that will sort from a string array.

I assume you mean "function", not "program" here.

Please post the functions, or at least the sort function. My guess is that you need to write your own compare function to override the compare function from string. Your compare function should compare the substrings AFTER the hyphen. The find function from string will help here.

http://www.cplusplus.com/reference/string/string/find/

There's a good example on using find and substr to get a substring here. Replace "live" with "-".

http://www.cplusplus.com/reference/string/string/substr/

Then use compare from string:

http://www.cplusplus.com/reference/string/string/compare/

int CompareTwoStrings (string fullString1, string fullString2)
// fullString1 is "Joshua Lynn Odom -  Bates Town".
{
    string stringAfterHyphen1, stringAfterHyphen2;
    // use find and substr on fullString1 and fullString2 to get
    // stringAfterHyphen1 and stringAfterHyphen2, respectively
    // stringAfterHyphen1 is " Bates Town".

    return stringAfterHyphen1.compare (stringAfterHyphen2);
}

Use the function above in your sort.

Write a comparison function that compared the substrings of each string.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctype.h>
using namespace std;

string getsubstr(const string& s)
{
    size_t pos = s.find('-');
    string v1 = s.substr(pos+1);
    // now remove any leading white space characters
    while( isspace(v1[0]) )
    {
        v1 = v1.substr(1);
    }
    return v1;
}
struct myclass {

    bool operator()(const string& s1, const string& s2)
    {
        string v1 = getsubstr(s1);
        string v2 = getsubstr(s2);
        return v1 < v2;
    }
} myobject;


int main()
{
    vector<string> towns;
    towns.push_back("Joshua Lynn Odom -  Bates Town");
    towns.push_back("Cindy Blue - Broken Arrow");
    towns.push_back("Donnie Kay - Oklahoma City");
    towns.push_back("Billy Bob - Durant");
    towns.push_back("Randall John - Lawton");
    towns.push_back("Danny Steven Ray - George Town");
    towns.push_back("Rodney White - Lone Grove");
    towns.push_back("Tammy Smiley - Ada");
    std::sort(towns.begin(), towns.end(), myobject);

    vector<string>::iterator it = towns.begin();

    for( ; it != towns.end(); it++)
        cout << *it << "\n";
}

Thank you so very much! I will study this code and rewrite with different examples. Thank you for all your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.