Need explanation on the code

#include<bits/stdc++.h>
using namespace std;
bool compare(string a,string b)
{
if(a.length()<=b.length() && b.substr(0,a.length())==a)
return true;
//if(a.length()>=b.length() && a.substr(0,b.length())==b)
//return true;
else
return a<b;
}
int main()
{
int t;
cin>>t;
string s[t];
for(int i=0;i<t;i++)
cin>>s[i];
sort(s,s+t,compare);
for(int i=0;i<t;i++)
cout<<s[i]<<"\n";
return 0;
}

if the input is
apple
bat
batman
just explain the full code taking the example

when you compare “apple” with “bat” compare() gives 1
when you compare “apple” with “batman” compare() gives 1
when you compare “bat” with “batman” compare() gives 1
if compare(a,b) returns 1(or True), it implies that a should come before b
if compare(a,b) returns 0(or False), it implies that a should come after b

if(a.length()<=b.length() && b.substr(0,a.length())==a) //first you compare their lenghts, and then if a occur as a prefix in b.
return true;
return a<b; //it checks the string lexicographically

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.