Facebook
From Diminutive Finch, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 188
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. struct Item {
  8.     char name[24];
  9.     int value;
  10. };
  11.  
  12.  
  13. bool compareValues(Item const &l, Item const &r) {
  14.     return l.value < r.value;
  15. }
  16.  
  17.  
  18. void printElement(Item const &x) {
  19.     cout << "name:" << x.name << ", value:" << x.value << '\n';
  20. }
  21.  
  22.  
  23. void printArr(Item const *arr, int n) {
  24.     for(int i = 0; i < n; i++) {
  25.         printElement(arr[i]);
  26.     }
  27. }
  28.  
  29.  
  30. int main()
  31. {
  32.     //Item A = {"potato", 8};
  33.  
  34.     Item arr[3] = {{"aaa", 33}, {"bbbb", 222}, {"c", 1}};
  35.     int n = 3;
  36.  
  37.     printArr(arr, 3);
  38.     cout << '\n';
  39.  
  40.     sort(arr, arr + 3, compareValues);
  41.  
  42.     printArr(arr, 3);
  43.     cout << '\n';
  44.  
  45.     return 0;
  46. }
  47.