fork download
  1. #include <algorithm>
  2. #include <cstring>
  3. #include <iomanip>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. struct List {
  9. char word[20];
  10. int repeat;
  11. };
  12.  
  13. int main() {
  14. List array[] = { { "as", 6 }, { "a", 1 }, { "appetite", 1 }, { "angry", 1 }, { "are", 2 }, { "and", 4 }, { "b", 1 } };
  15.  
  16. for(const auto i : array) cout << setw(15) << left << i.word << i.repeat << endl;
  17. sort(begin(array), end(array), [](const auto& lhs, const auto& rhs){ return *lhs.word < *rhs.word || *lhs.word == *rhs.word && (strlen(lhs.word) < strlen(rhs.word) || strlen(lhs.word) == strlen(rhs.word) && strcmp(lhs.word, rhs.word) < 0); });
  18.  
  19. for(const auto i : array) cout << setw(15) << left << i.word << i.repeat << endl;
  20. }
Success #stdin #stdout 0s 4360KB
stdin
Standard input is empty
stdout
as             6
a              1
appetite       1
angry          1
are            2
and            4
b              1
a              1
as             6
and            4
are            2
angry          1
appetite       1
b              1