Time limit exceeded error

#include
#include
#include
using namespace std;

bool comparator(string first,string second)
{
string one = first+second;
string two = second+first;

int i=0;
while(one[i] && two[i])
{
    if(one[i]>two[i])
        return true;
    else if(one[i]<two[i])
        return false;
    ++i;
}

return false;

}

int main() {
int tc;
cin>>tc;
while(tc–)
{
int len;
vector arr;

   int i;
   string temp;
   cin>>len;
   for(i=0;i<len;++i)
   {
       cin>>temp;
       arr.push_back(temp);
   }
   
   sort(arr.begin(),arr.end(),comparator);
    
   for(i=0;i<len;++i)
       cout<<arr[i]<<" ";
   cout<<"\n";
}
return 0;

}

hello @igarg145

replace this with return one> two ,it will pass.

Yes it passed the test cases, but I didn’t understand the logic. Please could you explain.

yours is also correct, just make sure u r not printing space between two numbers.