I am writing c++ to sort a string vector. The string should be sorted by its length. If the lengths are the same, sorting by its lexicographical order: eg: abc < abd Here is my code:
static bool sort_find(string& a, string& b){
if(a.size() < b.size()){
return true;
}
else if(a.size() > b.size()){
return false;
}
else{
for(int i=0;i<a.size();i++){
if(a[i] == b[i] )
continue;
else if(a[i] < b[i]){
return true;
}
else{
return false;
}
}
return true;
}
}
int main(){
string array[13]={"m","mo","moc","moch","mocha","l","la","lat","latt","latte","c","ca","cat"};
vector<string> svector(array,array+13);
sort(svector.begin(),svector.end(),sort_find);
return 0;
}
The output of this code is [c, l, m, ca, la, moch, mo, cat, lat, moc, latt, mocha, latte] The output does not make sense to me.
Everyone's help is welcome!!
Thanks!