Segmentation error or something

as far as i can tell, in this question we have to sot the numbers in decreasing lexicographical order. i don’t know what function to use for that. so I searched on the net and found this code. why is this not working, and also, how do I sort these numbers lexicographically?


#include
using namespace std;

int main()
{
int t;
cin>>t;

while (t>0)
{
	int n;
	cin>>n;
	string str[n], temp;

	cout << "Enter 10 words: " << endl;
	for(int i = 0; i < n; ++i)
	{
  	getline(cin, str[i]);
	}

	// Use Bubble Sort to arrange words
	for (int i = 0; i < n; ++i) {
    	for (int j = 0; j < n - i; ++j) {
        	if (str[j] > str[j + 1]) {
            	temp = str[j];
            	str[j] = str[j + 1];
            	str[j + 1] = temp;
        	}
    	}
	}

	cout << "In lexicographical order: " << endl;

	for(int i = 0; i < 10; ++i)
	{
   
   		cout << str[i] << endl;
	}
t--;
}	
return 0;

}

hello @yamika

no sorting by decrezsig order will not ensure correct result.
example->

21 and 2
if u sort in decreasing order then
the number formed will bw 212 but the correct result is 221.
this code is failing for the same case.

u can use sort function to sort tge input in ascending order.if u want to make any chsanges in sorting order then u need to pass comparator.

for this the logic is if a and b are two numbers then u can combine them in two ways either ab or ba right?
which ever combination give us maxium we will consider that order.
so just apply same logic in ur comparator and sort using this comparator

bool cmp(string a,string b){
return a+b > b+a;
} 

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.