In this question half of the things i done , but i can't able to do that if "bat" is the prefix of "batman'" then how i sort string according to this condition

public class App04_StringSort {

public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int size = sc.nextInt();
	String[] arr = new String[size];
	
	for (int i = 0; i < arr.length; i++) {
		arr[i]=sc.next();
	}
	

	for (int i = 0; i < arr.length-1; i++) {
		if((arr[i].compareTo(arr[i+1])>0) ) {
			String temp = arr[i];
			arr[i] = arr[i+1];
			arr[i+1]=temp;
		}
	}
	
	for (int i = 0; i < arr.length; i++) {
		System.out.println(arr[i]);
	}
}

}

@nigamshubham1998,
You need to define your own comparator for this problem.
Something like this:

        int i = 0;      

        while (i < s1.length() && i < s2.length()) {

            if (s1[i] > s2[i]) {

                return 1;
            } else if (s1[i] < s2[i]) {
                return -1;
            }
            i++;

        }

        if (s1.length() > s2.length()) {
            return -1;
        } else {
            return 1;
        }

Compare the characters one by one and make sure that i < s1.length() && i < s2.length(). If there exists a prefix, the next ’ if ’ condition will take care of it.

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.