Hi!
I have a problem to make a list of students (i know how to put all the input and to calculate the average of the students grades) but i don't know how to put the students in new list arrayed by their average form biggest to smallest and if two students have same average grade then they should array by other input data.Please help me!

2
Contributors
1
2
Views
8 Years
Discussion Span
Last Post by Narue

What you want is a multi-key search. If the grades match, compare another key. One way is to do it directly. That way you have more control over ascending and descending. For example if you want to sort the grade descending, but the other key (like a name) ascending:

``````#include <algorithm>
#include <iostream>
#include <string>

struct Student {
std::string name;
};

bool multikey_compare ( const Student& a, const Student& b )
{
return a.name < b.name;

}

int main()
{
Student list[] = {
{"Joe Blow", 80},
{"Jane Doe", 80},
{"Fonzie", 60},
{"The Dude", 70},
{"Some Guy", 80},
{"Z Man", 90},
{"John Doe", 90},
{"A Man", 70}
};

std::sort ( list, list + 8, multikey_compare );

for ( int i = 0; i < 8; i++ )
}``````

If both keys are to be sorted the same way, you can concatenate them into a string and then compare the string:

``````#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>

struct Student {
std::string name;
};

bool multikey_compare ( const Student& a, const Student& b )
{
std::ostringstream aout;
std::ostringstream bout;

return aout.str() > bout.str();
}

int main()
{
Student list[] = {
{"Joe Blow", 80},
{"Jane Doe", 80},
{"Fonzie", 60},
{"The Dude", 70},
{"Some Guy", 80},
{"Z Man", 90},
{"John Doe", 90},
{"A Man", 70}
};

std::sort ( list, list + 8, multikey_compare );

for ( int i = 0; i < 8; i++ )