fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. struct Node {
  6. int i;
  7. char ch;
  8. };
  9.  
  10. //Custom comparator
  11. bool cmp (const Node &a, const Node &b){
  12. return a.i < b.i;
  13. }
  14.  
  15. int main() {
  16.  
  17. int N = 3;
  18.  
  19. Node a[N];
  20. a[0].i = 2;
  21. a[0].ch = 'b';
  22. a[1].i = 1;
  23. a[1].ch = 'a';
  24. a[2].i = 3;
  25. a[2].ch = 'c';
  26.  
  27. sort(a, a+N, &cmp);
  28.  
  29. for (int i=0; i<N; i++) {
  30. cout<<a[i].i<<" "<<a[i].ch<<endl;
  31. }
  32.  
  33. return 0;
  34. }
Success #stdin #stdout 0s 5416KB
stdin
Standard input is empty
stdout
1 a
2 b
3 c