Doubt in using comparator to sort

#include <bits/stdc++.h>
using namespace std;

bool compare(string a,string b)
{

int len1=a.size();
int len2=b.size();
int i=0,j=0;
bool check=true;
while(len1 && len2)
{
    if(a[i]!=b[j])
    {
        check=false;
        break;
    }
    i++;
    j++;
    len1--,len2--;
}
if(!check)return a.size()>b.size();
else
 return a<b;

}

int main()
{
int n;
cin>>n;
string str[n];
for(int i=0;i<n;++i)cin>>str[i];
sort(str,str+n,compare);
for(int i=0;i<n;++i)cout<<str[i]<<endl;
return 0;
}

@nikhilsnghchauhan Check the approach
For this question you can make an array of strings and then sort it according to the condition that if there is a prefix match between two string, sort in decreasing order of length. Use C++ inbuilt sort function with a custom comparator something like this:

bool mycompare( string a, string b)
{
int l1=a.length();int l2=b.length();int flag=-1;
for(int i=0;i<min(l1,l2);i++){
if(a[i]!=b[i])
flag=0;
}
if(flag==-1) // means there is a prefix match
return l1>l2; // so sort decreasing order of length
else
return a<b; //sorting in normal dictionary order when no prefix match
}

Implement it like this. If still your doubt is not resolve then please save your code on ide.codingblocks.com and share its link. It is easy for us to debug then.