Bottom Posted reply
Originally Posted by
Daved
The best way in C++ would be to define two sorting predicates for your class. One that sorts based on the name and one that sorts based on the tuition. This is just a function that returns a bool and takes two (const) references to your class. It should return true if the frst argument is less than the second, and false if they are equal or if the second argument is less than the first. These function should be stand-alone functions, not part of your class.
Then, you can sort your array (or vector which is preferred over arrays in C++) using std::sort and passing in the sorting predicate you want to use - either the one on the name or the one on the tuition.
You should probably be using the C++ string class to hold your names. If you are not, then this solution might be more complicated since you would have to add copy-ability to your class.
Does the standard std::sort sort the whole class? simple case listed
Code:
Class School
{
public
string name;
float tuition;
};
int main()
{
School mySchool [5] = {School ('John", 10.),
School ('Jim", 11.),
School ('Greg", 12.),
School ('Spencer", 13.),
School ('Joan", 14.)};
/* enter sort syntax here Can I use std::sort and how?*/
myFunctionSortSyntax ("Sort my mySchool.name");
myPrintSortedLists ("Print the Sorted with with name and tuition");
return EXIT_SUCCESS;
}