-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_classes_per_attribute.cpp
58 lines (46 loc) · 1.01 KB
/
sort_classes_per_attribute.cpp
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
56
57
58
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
class Entity
{
public:
Entity();
Entity(double x_, double y_, float speed_) : x(x_), y(y_), speed(speed_){};
float speed;
double x, y;
void printEntity()
{
cout << "x:" << x << " y:" << y << " velocity:" << speed << endl;
}
};
bool speedCmp(Entity const &a, Entity const &b)
{
if (a.speed == b.speed)
{
return a.x > b.x;
};
return a.speed < b.speed;
}
int main()
{
Entity e1 = Entity(1, 2, 3.55);
Entity e2 = Entity(4, 2, 3.55);
Entity e3 = Entity(6, 3, 1.55);
Entity e4 = Entity(-1, -2, 3.0);
Entity e5 = Entity(0, 0, 1.8);
Entity entities[] = {e1, e2, e3, e4, e5};
cout << " before " << endl;
for (Entity &e : entities)
{
e.printEntity();
};
std::sort(entities, entities + 5, speedCmp);
cout << " after " << endl;
for (Entity &e : entities)
{
e.printEntity();
};
}