String sort...substring problem

my code is this :
plase help me to deal with the problem when a string is substring of another
String[] arr= {“fafsd”,“fsfs”,“sdgsdg”,“dgsdg”,“dsgsdg”,“fsfsad”};

	for(int counter=0;counter<arr.length-1;counter++)
	{
		int smallest=counter;
		for(int i = counter+1;i<=arr.length-1;i++)
		{
			int index=0;
			while(arr[i].charAt(index)==arr[smallest].charAt(index))
			{
				index++;
				
			}

//
// if(arr[i].length()>arr[smallest].length())
// {
// smallest=i;
// }
if(arr[i].charAt(index)<arr[smallest].charAt(index))
{
smallest=i;

			}
			
		}
		String temp = arr[smallest];
		arr[smallest] = arr[counter];
		arr[counter] = temp;
	}
	System.out.println(Arrays.toString(arr));

see there were some changes:

System.out.println(Arrays.toString(arr)); this will not work…so you need to print each string in a new line

i have made some changes in your code with comments
refer this code

your logic is fine…


still there is some error

pls refer to above code you have not incorporated all the changes…see when index of one of the strings become equal to its length we need to break…moreover your code is printing some random values…remove them

still showing error

showing error …arrayoutofbound

in line no 26 and 43 of your code replace counter with smallest…

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.