Problem with sorting struct

Jan 11, 2017 at 5:56pm
So my program is working until it get to sorting part and then it crash.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

#include <iostream>     
#include <algorithm> 
#include <string>
#include <cstdlib>

using namespace std;

struct prezenty {

	string imie;
	int x, y;

};

bool sortowanie(prezenty lhs, prezenty rhs) {

	if (lhs.x <= rhs.x && lhs.y > rhs.y) return rhs.x < lhs.x;
	if (lhs.x >= rhs.x && lhs.y < rhs.y) return lhs.x > rhs.x;
	 if (lhs.x < rhs.x&&lhs.y < rhs.y) return lhs.x < rhs.x;
	if (lhs.x > rhs.x&&lhs.y > rhs.y) {
		if (lhs.x > lhs.y) return lhs.x < rhs.x;
		 if (lhs.x < lhs.y) return lhs.x > rhs.x;
	}
	else return lhs.x < rhs.x;




	}
	



int main() {



	prezenty dzieci[4];
	
	for (int i = 0; i < 4; i++) {
		cout << "Podaj imie: ";
		cin >> dzieci[i].imie;
		cout << "Podaj lokalizacje x: ";
		cin >> dzieci[i].x;
		cout << "Podaj lokalizacje y: ";
		cin >> dzieci[i].y;



	}
	sort(dzieci, dzieci+4, sortowanie);

	system("pause");
}
Jan 11, 2017 at 9:08pm
Please explain all the logic in your sortowanie() function.

By the way IMO your trying to do too many comparisons in that function, and you really should stick with either less than or equal to or less than operators and try to use only one return statement. By the way you may have a path that doesn't match your logic and bypasses all the if/else chains and then the function returns "garbage".

Look at some documentation for the comp function used in std::sort:
comp Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines.

The highlighted portion of the quote is why you should stick to less than.
Jan 11, 2017 at 10:56pm
If lhs.x > rhs.x and lhs.y > rhs.y then the program gets to lines 22 & 23. Now if lhs.x == lhs.y then neither condition is true and the program goes to line 30 where the function exits without setting a return value. That will most certainly cause a problem.

Like jlb said, please explain in words what the ordering should be. I strongly suspect that the code doesn't reflect what you're trying to do.
Jan 12, 2017 at 1:04am
So obiects of "prezenty" represent a point on coordinate system and im trying to sort them from top left to bottom right.
Jan 12, 2017 at 4:15am
closed account (48T7M4Gy)
5:5-5
3:2-5
0:3-1
2:1-3
6:1-1
7:0-0

7:0-0
6:1-1
2:1-3
0:3-1
3:2-5
5:5-5
Program ended with exit code: 0
Last edited on Jan 16, 2017 at 5:36am
Jan 12, 2017 at 5:33am
This predicate used in conjunction with std::sort engenders undefined behaviour.
1
2
3
4
bool left_of(const Point lhs, const Point rhs)
{
    return ( (lhs.x * lhs.x + lhs.y * lhs.y) <= (rhs.x * rhs.x + rhs.y * rhs.y) );
}

strict weak ordering: "the term strict refers to the requirement of an irreflexive relation." - IS
Jan 12, 2017 at 6:17am
closed account (48T7M4Gy)

5:5-5
3:2-5
0:3-1
2:1-3
6:1-1
7:0-0

7:0-0
6:1-1
0:3-1 
2:1-3
3:2-5
5:5-5
 
Exit code: 0 (normal program termination)
Last edited on Jan 16, 2017 at 7:55am
Jan 12, 2017 at 5:56pm
Something like this will probably do:
1
2
3
4
5
6
7
8
9
10
// sort by x, then by y
bool
sortowanie(const prezenty &lhs, const prezenty &rhs)
{
    if (lhs.x < rhs.x) return true;
    if (lhs.x > rhs.x) return false;

    // x values are equal. Break the tie with the y value
    return lhs.y < rhs.y;
}
Topic archived. No new replies allowed.