fork download
  1. //#include <iostream>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. bool comparator(string first,string second)
  6. {
  7. string one = first+second;
  8. string two = second+first;
  9.  
  10. int i=0;
  11. while(one[i] && two[i])
  12. {
  13. if(one[i]>two[i])
  14. return true;
  15. else if(one[i]<two[i])
  16. return false;
  17. i++;
  18. }
  19.  
  20. return false;
  21. }
  22.  
  23. int main() {
  24. //code
  25. int tc;
  26. cin>>tc;
  27. while(tc--)
  28. {
  29. int len;
  30. vector<string> arr;
  31.  
  32. //Take Array inputs
  33. int i;
  34. string temp;
  35. cin>>len;
  36. for(i=0;i<len;++i)
  37. {
  38. cin>>temp;
  39. arr.push_back(temp);
  40. }
  41.  
  42. sort(arr.begin(),arr.end(),comparator);
  43.  
  44. for(i=0;i<len;++i)
  45. cout<<arr[i]<<" ";
  46. cout<<"\n";
  47. }
  48. return 0;
  49. }
Success #stdin #stdout 0.01s 5452KB
stdin
3
8
49 73 58 30 72 44 78 23 
4
69 9 57 60 
2
40 4 
stdout
78 73 72 58 49 44 30 23 
9 69 60 57 
4 40