I met a problem in sorting a file containing astudents fistnane, secondname and GPA.if I sort the file using first name or lastname their Gpa will be miss placed please help me.
the file is:
kalayu haile 3.5
fikadu adisu 3.99
eyob assefa 3.25
getaneh allesew 3.75
fraol gezahegn 3.00
jim broun 4.00

Recommended Answers

All 11 Replies

You have to decide whether you want to sort the file by name or by GPA. If you want it sorted alphabetically then sort by last name (or second name) then first name. If you want it sorted by GPA then sort by gpa, last name and first name.

To do that I would read all the lines into a structure to make sorting easier.

struct data
{
   string first;
   string last;
   float gpa;
};

Create an array of those structures then sort them however you wish.

I met a problem in sorting a file alphabetically. the file contains a students firstnane, fathersname and GPA.if I sort the file using first name or fathersname; their Gpa will not be correct. if
the file is:
kalayu haile 3.5
fikadu adisu 3.99
eyob assefa 3.25
getaneh allesew 3.75
fraol gezahegn 3.00
jim broun 4.00
so to keep the GPA correct what should I do?I need your help.

If you store your new structs that AncientDragon suggested in a std::vector, you can then use sort() from STL <algorithm>

Dave

If I sort it using GPA; the file will not be sorted alphabetically.

If you have the info in a struct for each person, then no matter what your sorting criterion, the info fill stay together.

If I sort it using GPA; the file will not be sorted alphabetically.

Of course not -- that's what sorting is all about. Your sort algorithm will have to be smart enough to sort alphabetically within the same gpa. For example if two people have the same gpa then the two people's names need to be alphabetical.

The way you describe the problem suggests that you're using parallel arrays to store the data.

vector<string> firstname;
vector<string> fathersname;
vector<double> gpa;

That is a risky design because you need to write code to keep the related fields together. It would be easier to read a line and parse it into a record object, then store those objects in an array. You can sort based on one of the fields in the object.

class Student {
public:
    Student(string const& raw);
public:
    string firstname, fathersname;
    double gpa;
};

bool CompareFirstname(Student const& a, Student const& b)
{
    return a.firstname < b.firstname;
}

while (getline(file, line))
    students.push_back(Student(line));

sort(students.begin(), students.end(), CompareFirstname);

I have met a proplem in sorting a file containg a customer's first name, second name, country and registration number using first name in random access file.
the file is:
fikadu adisu USA 1
kalayu haile Ethiopia 2
fraol getu Canada 3

The approach will be exactly the same as in your other thread (http://www.daniweb.com/forums/thread292268.html).

Are you having a specific problem implementing what those other posters suggested?

I cannot understand what the programm .i

Threads merged

commented: Thanks Peter! +4
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.