compare 3 objects and return 1, -1, or 0

i have 3 objects, first name, lastname, units done, credits earned

well the all have different units and credits, so im supposed to compare all three and return a 1 for best, -1 for least, and 0 for same

0 mean if they have the same credits THEN gpa THEN units THEN they both return a 0 for being the same.

now, how i am supposed to compare 3, when i can only compare 2 at a time.

int compare(const studentType &other) const;
so its like
1
2
3
4
5
6
7
 
student1.compare(student2);
student1.compare(student3);
2 to 1;
2 to 3;
3 to 1;
3 to 2;

but i think this above code is to long, i need to compare one by one, and i dont know what the return value has todo with outputting them in order.
i remeber the proffesor showing somethig like this:
(studen1.compare(student2)).student3 <---- this would compare 3 objects by somehow fooling with the order or something like that

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int studentType::compare(const studentType &other) const
{

	if (studentType::myearned > other.myearned  )
		return 1;
	else if (studentType::myearned  < other.myearned )
		return -1;
	else if (studentType::myearned  == other.myearned) 
	{
		if (studentType::gpa()  > other.gpa())
			return 1;
		else if (studentType::gpa()  < other.gpa())
			return -1;
		else if (studentType::gpa()  == other.gpa())
		{
			if (studentType::mydone > other.mydone)
				return 1;
			else if (studentType::mydone < other.mydone)
				return -1;
			else
				return 0;
		}
	}

}


so what do you guys/girls think about displaying them in order
If you overload operator<(const studentType &other) then you can use std::max_element :
http://www.cplusplus.com/reference/algorithm/max_element/

If I'm not mistaken then your overload would look like this :
1
2
3
bool studentType::operator<(const studentType &other) {
    return compare(other) < 0;
}
Put your student objects in an array or vector, implement the less than function R0mai showed you, then use the sort() algorithm included in the STL. Once this is done you can print them.
Topic archived. No new replies allowed.