fork download
  1.  
  2. #include <iostream>
  3. #include <algorithm>
  4.  
  5. struct Data {
  6. char name;
  7. int value;
  8. };
  9.  
  10. bool compare_data(const Data& first, const Data& second) {
  11. return first.value < second.value;
  12. }
  13.  
  14. int main() {
  15.  
  16. Data foo[4];
  17.  
  18. foo[0].name = 'A';
  19. foo[0].value = 10;
  20.  
  21.  
  22. foo[1].name = 'B';
  23. foo[1].value = 15;
  24.  
  25.  
  26. foo[2].name = 'C';
  27. foo[2].value = 11;
  28.  
  29.  
  30. foo[3].name = 'D';
  31. foo[3].value = 9;
  32.  
  33. std::sort(std::begin(foo), std::end(foo), compare_data);
  34.  
  35. for (int i = 0; i < 4; i++) {
  36. std::cout<<foo[i].name<<std::endl;
  37. }
  38.  
  39. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
D
A
C
B