Sort CCArray of strings in alphabetical order.


#1

hello all….
i am developing project for iphone and android both in cocos2dx…i have 1 ccarray of names i want to make it appear in alphabetical order on the screen,plz help i am new in cocos…


#2

Use std::sort with custom functor.

std::sort(sorted->data->arr, sorted->data->arr + sorted->data->num, comp);


#3

Hi sergey thanks for your reply… i am trying to use this but i am getting 9 semantic issue in stl_algo.h and stl_heap.h file…may be i am mistake in comparator plz tell me how to use it correctly….


#4

Check that comparator have signature (CCObject *, CCObject *) -> bool.


#5

thanks…yes my comparator has different signature have look on this….

static bool compareCCString(const CCString* p1, const CCString* p2)
{
return strcmp(p1~~>getCString,p2~~>getCString());
}

if i convert it to ccobect then how can i make comparison of string…


#6

hello i change it as u said…

static bool compareCCString(const CCObject* p1, const CCObject* p2)
{
const char* obj = (const char**)p1;
const char** obj1 = (const char*)p2;
return strcmp(obj,obj1);

}
and i am calling as…
std::sort(allKeys~~>data~~>arr, allKeys~~>data~~>arr + allKeys~~>data~~>num, compareCCString);
where allkeys are CCArray
still result is not correct


#7

Don’t cast const CCObject * to const char *. Use static_cast to easily detect such errors.


#8

hi sergey thanks for your help…
i got the result finally…
now i am calling strcmp function inside of comparator function instead of these i am comparing string on my own…
here is my code…
static bool compareCCString(const CCObject* p1, const CCObject* p2)
{

CCString* obj = (CCString**)p1;
CCString** obj1 = (CCString*)p2;
int temp_length1 = sizeof(obj->getCString());
int temp_length2 = sizeof(obj1->getCString());
int i=0;
while ((i < temp_length1) && (i < temp_length2))
{
if (tolower (obj->getCString()[i]) < tolower (obj1->getCString()[i]))
return true;
else if (tolower (obj->getCString()[i]) > tolower (obj1->getCString()[i]))
return false;
i++;
}
return false;
}
again thanks


#9

sizeof returns size of pointer, but not size of underlying string.