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. void printArr (Node *a, int N) {
  16.  
  17. for (int i=0; i<N; i++) {
  18. cout<<a[i].i<<" "<<a[i].ch<<endl;
  19. }
  20.  
  21. }
  22.  
  23. int main() {
  24.  
  25. int N = 3;
  26.  
  27. Node a[N];
  28. a[0].i = 2;
  29. a[0].ch = 'b';
  30. a[1].i = 1;
  31. a[1].ch = 'a';
  32. a[2].i = 3;
  33. a[2].ch = 'c';
  34.  
  35. cout<<"Array before sorting:"<<endl;
  36. printArr(a, N);
  37.  
  38. sort(a, a+N, &cmp);
  39.  
  40. cout<<"Array after sorting:"<<endl;
  41. printArr(a, N);
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0.01s 5520KB
stdin
Standard input is empty
stdout
Array before sorting:
2 b
1 a
3 c
Array after sorting:
1 a
2 b
3 c