Please check this piece of code|| wrong answer

Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t>0){
int N=sc.nextInt();
String ss=sc.nextLine();
String[] arr=new String[N];
for(int i=0;i<N;i++){
arr[i]=sc.nextLine();
}

		t--;
		String j,k;
		String ans=arr[0];
		for(int i=1;i<N;i++){
			j=ans+""+arr[i];
			k=arr[i]+""+ans;
			if(j.compareTo(k)>0){
				ans=String.valueOf(j);
			}
			else{
				ans=String.valueOf(k);
			}
		}
		System.out.println(ans);
	}
	sc.close();

@Rajput.apry hi, your approach is incorrect, yes that comparison part you tried is good but still it will not work:-
Check for the test case:-
1
4
9 98 3 7

your output :- 99837
Correct:- 99873

then what should I try for??Any hint!!!

@Rajput.apry Hi, basically you have to use sorting, it would be easy! You can do custom sort by passing an anonymous comparator to it, rest will remain same, the compare thing!

I am attaching my code as reference, just check it out.


If you still have query, feel free to ask, else resolve the doubt and rate!