Doubt related string sort


if a string is completely as a prefix in another string so longer string should come first how we will sort it?

While comparing two strings using compareTo function, you could compare the k lengths of both the strings as well. k here denotes the minimum lengths of the two strings to be compared. Now, if it returns 0, that means that one string is a prefix of another string. In such a case, just take the longer string first otherwise, just use the normal comparator function that you are currently using in your program.

please elaborte it ,i am not able to understand what are you trying to say?

if two strings are “a” & “ab”, just take minimum length from both strings i.e “a” and “a”. if they are same, then place longer one first i.e. “ab” else use your normal comparator fn on both original strings

can you please share the code ?

No, first you try to do it. I can point out the improvements in your code but telling you the entire code won’t get you anything. I’ve already told you the entire approach in detail.

https://ide.codingblocks.com/s/169814 this is my code ,improve it?

Here it is

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.