summaryrefslogtreecommitdiff
path: root/c,cc/sort2.cc
blob: 3d64952b57acc354b4944c2e937880022c35c862 (plain)
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
59
60
61
62
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>

using namespace std;


struct X
{
  string n;
  int a;
  int b;
};

bool cmph(X a, X b)
{
  return (a.a+a.b) > (b.a+b.b);
}

bool cmpf(X a, X b)
{
  return (a.a-a.b) > (b.a-b.b);
}

int main(void)
{
  X a;
  a.n = "a";
  a.a = 1;
  a.b = 2;

  X b;
  b.n = "b";
  b.a = 2;
  b.b = 0;

  X c;
  c.n = "c";
  c.a = 4;
  c.b = -1;

  vector<X> v;
  v.push_back(a);
  v.push_back(b);
  v.push_back(c);

  for (unsigned i = 0; i < v.size(); i++) {
    cout << v[i].n << " " << v[i].a << " " << v[i].b << endl;
  }
  sort(v.begin(), v.end(), cmph);
  cout << endl;
  for (unsigned i = 0; i < v.size(); i++) {
    cout << v[i].n << " " << v[i].a << " " << v[i].b << endl;
  }
  sort(v.begin(), v.end(), cmpf);
  cout << endl;
  for (unsigned i = 0; i < v.size(); i++) {
    cout << v[i].n << " " << v[i].a << " " << v[i].b << endl;
  }
}