One test case for String sort is failing

In the problem String Sort the first test case is passed but the second test case is failing. Here is my code

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String [] strArr = new String[n];
for(int i=0; i<n; i++)
{
String s = sc.next();
strArr[i] = s;
}
for(int i=0; i<n; i++)
{
for(int j=0; j<n-1-i; j++)
{
if (strArr[j].compareTo(strArr[j+1])>0)
{
String temp = strArr[j];
strArr[j] = strArr[j+1];
strArr[j+1] = temp;
break;
}
}
}
for(int j=0; j<strArr.length; j++)
{
for(int i=0; i<strArr.length-1; i++)
{
int len1 = strArr[i].length();
int len2 = strArr[i+1].length();
if((len2-len1>0) && (strArr[i+1].substring(0, len1).equals(strArr[i])))
{
String temp = strArr[i+1];
strArr[i+1] = strArr[i];
strArr[i] = temp;
}
}
}

    for(int i=0; i<strArr.length; i++)
    {
        System.out.println(strArr[i]);        
    }
}

}

@S19MLPP0010
Removal of break statement inside swap block will certainly do the work.

1 Like

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.