sir, i took this code from solution. https://ide.codingblocks.com/s/453269. But, i have one doubt in line no. 22 to 26. if the length of any string is greater than the other string. then it should be “1” return.
I have doubt in line number 22 to 26
hello @chandreshmaurya
if (compareTo(arr[j], arr[j + 1]) > 0) {
string temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
in this line they are using compare function .
now note one thing , they are performing swapping only when compareto return positive value.
if (s1.length() > s2.length()) {
return -1;
} else {
return 1;
}
the control will reach to this if elsse only if while loop writen above will execute completely.
its compete execution indicates that one string is prefix or other string.
now for that case , we want to put larger string first and then smaller string.
so if larger string is already before smaller string no need to perform swap hence we are returning -1.
otherwise returning 1
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.