How can I sort lines in a text file based on the third word?

被刻印的时光 ゝ 提交于 2020-03-17 03:30:44

问题


What I am trying to do is sort the students' details in a text file based on their first name, id, results and grades.

Here is what my Text_File.txt looks like: (each word is separated by tab '\t')

rose     ramzi     154ss6    85    A
john     watson    123aq1    96    A+
ellen    thomas    qs2115    75    B+

I am trying to sort the lines in my text file in ascending order based on the third word (which is the student's ID).

I was able to sort the list with the first name and also I could extract the third word and tried using sort() function but it is still not sorting by ID.

Here is my sorting_by_id code:

int main() {
    fstream input("Text_File.txt", ios::in | ios::out);
    string line;
    vector<string> lines;


    while (getline(input, line))
        lines.push_back(line);
    sort(lines.begin(), lines.end(), [](const string& a, const string& b) {
        int wordStartPositionA = 0, wordStartPositionB = 0;//The start of each word in the string

        for (int i = 0; i < 2; i++)//looking for the third word in 1st sentence
            wordStartPositionA = a.find_first_of('  ', wordStartPositionA + 1);

        for (int i = 0; i < 2; i++)//looking for the third word in 2nd sentence
            wordStartPositionB = b.find_first_of('  ', wordStartPositionB + 1);

        //Getting where our word ends:
        int wordEndPositionA = a.find_first_of('    ', wordStartPositionA + 1);
        int wordEndPositionB = b.find_first_of('    ', wordStartPositionB + 1);

        //Getting the desired word
        string resultA = a.substr(wordStartPositionA + 1, a.length() - wordEndPositionA - 1);
        string resultB = b.substr(wordStartPositionB + 1, b.length() - wordEndPositionB - 1);

        //cout << resultA << "  " << resultB << endl;

        return resultA < resultB;

    });
    for_each(lines.begin(), lines.end(), [](const string& s) {
        cout << s << endl; });

    getchar();
    return 0;
}

My aim is to sort the students based on their ID number.

Thank you for your help :)


回答1:


You need some data abstraction.

First define a suitable data type:

struct Student
{
    std::string firstName;
    std::string lastName;
    std::string id;
    int result;
    std::string grade;
};

then you define how to read one of them:

std::istream& operator>>(std::istream& is, Student& s)
{
    return is >> s.firstName >> s.lastName >> s.id >> s.result >> s.grade;
}

then you read them:

int main() 
{
    std::ifstream input("Text_File.txt");
    std::vector<Student> students;
    Student s;
    while (input >> s)
    {
        students.push_back(s);
    }

then you can easily sort them by id:

std::sort(students.begin(), 
          students.end(), 
          [](const Student& s1, const Student& s2) { return s1.id < s2.id; });

or by decreasing result:

std::sort(students.begin(), 
          students.end(), 
          [](const Student& s1, const Student& s2) { return s1.result > s2.result; });

or by whatever.



来源:https://stackoverflow.com/questions/47630702/how-can-i-sort-lines-in-a-text-file-based-on-the-third-word

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!