How to add a Sort method to my wxArray Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
posta10100
Earned some good credits
Earned some good credits
Posts: 106
Joined: Tue Jan 09, 2007 2:27 pm
Location: Italy

How to add a Sort method to my wxArray

Post by posta10100 » Wed Apr 04, 2007 11:32 am

Hello!
I need to add a Sort method to my derived wxArray class.

I see that the header of method is

void Sort(CMPFUNC<T> compareFunction)

I need to add in my class something that accept a method (written in another class) like this:

Code: Select all

int wxCMPFUNC_CONV AClass::sortSegments(MySegment *s, MySegment *s1) {
    if (...) {
        return 0;
    }
    if (...) {
        return 1;
    }
    return -1;
}
and call the sort method of the wxArray class.

Someone can help me?

Thanks!!!

lowjoel
Moderator
Moderator
Posts: 1511
Joined: Sun Jun 19, 2005 11:37 am
Location: Singapore
Contact:

Post by lowjoel » Wed Apr 04, 2007 9:58 pm

Yes, your function is perfectly fine and legal. You may have problems with the this pointer however, since the sort function does not allow you to provide one when the object method is used from a pointer. You should change your sorting function to be a static member function if you want to use this approach, or you can use a global function.

Then, just call myArray.Sort(&AClass::sortSegments); if you are using a static member function, or myArray.Sort(sortSegments) if you are using a global function.

Joel

posta10100
Earned some good credits
Earned some good credits
Posts: 106
Joined: Tue Jan 09, 2007 2:27 pm
Location: Italy

Post by posta10100 » Thu Apr 05, 2007 6:36 am

What are you saying is that in the AClass class I have to declare a static method

Code: Select all

static int wxCMPFUNC_CONV AClass::sortSegments(MySegment *s, MySegment *s1);
and then I have to call the sort method in the myArray class declared as

Code: Select all

void Sort(CMPFUNC compareFunction);
with the command

Code: Select all

myArray *arr = new myArray();
arr->Add(...);
arr->Add(...);
arr->Add(...);
arr->Sort(&AClass::sortSegments);
Is it correct?

Thanks for your help!

lowjoel
Moderator
Moderator
Posts: 1511
Joined: Sun Jun 19, 2005 11:37 am
Location: Singapore
Contact:

Post by lowjoel » Thu Apr 05, 2007 10:12 am

Yep. Do you have problems using that?

Joel

posta10100
Earned some good credits
Earned some good credits
Posts: 106
Joined: Tue Jan 09, 2007 2:27 pm
Location: Italy

Post by posta10100 » Thu Apr 05, 2007 10:54 am

Yes, it don't work...

In myArray header file I put:

Code: Select all

WX_DECLARE_OBJARRAY(MySegment*, MySegmentArray);
...
void Sort(CMPFUNC compareFunction);
...
MySegmentArray *m_SegmentArray;
while in the cpp there is:

Code: Select all

void myArray::Sort(CMPFUNC compareFunction) {
    m_SegmentArray->Sort(compareFunction); //This is the line 70
}
But when I try to compile I have this error:

70 Z:\Luca\myArray.cpp invalid conversion from `int (*)(const void*, const void*)' to `int (*)(MySegment***, MySegment***)'

Again, thanks for your help!!!

Luca.

lowjoel
Moderator
Moderator
Posts: 1511
Joined: Sun Jun 19, 2005 11:37 am
Location: Singapore
Contact:

Post by lowjoel » Thu Apr 05, 2007 12:19 pm

Code: Select all

typedef int (*SegmentSortFunc)(MySegment***, MySegment***);
m_SegmentArray->Sort(reinterpret_cast<SegmentSortFunc>(compareFunction));
Hope that works (I use the STL so I don't have to use these weird sorting functions)

Joel

DavidHart
Site Admin
Site Admin
Posts: 4008
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Post by DavidHart » Thu Apr 05, 2007 1:22 pm

Hi,

This works for me.

Code: Select all

WX_DECLARE_OBJARRAY(class Foo, FooArray);

...

static int wxCMPFUNC_CONV StringCompareFunction(Foo **first, Foo **second)
{
return (*first)->GetString().CmpNoCase((*second)->GetString());
}

static int wxCMPFUNC_CONV IntCompareFunction(Foo **first, Foo **second)
{
return (*first)->Int > (*second)->Int;
}

// Usage:

FooArray array;
...
array.Sort( StringCompareFunction );  // Sort by string
// array.Sort( IntCompareFunction ); or by Int
Note that my wxArray is of objects, while yours is of pointers to objects (btw, is that allowed in WX_DECLARE_OBJARRAY? If you want pointers, why not use WX_DEFINE_ARRAY?)

Hope this helps.

Regards,

David

posta10100
Earned some good credits
Earned some good credits
Posts: 106
Joined: Tue Jan 09, 2007 2:27 pm
Location: Italy

Post by posta10100 » Thu Apr 05, 2007 1:45 pm

lowjoel wrote:

Code: Select all

typedef int (*SegmentSortFunc)(MySegment***, MySegment***);
m_SegmentArray->Sort(reinterpret_cast<SegmentSortFunc>(compareFunction));
Hope that works (I use the STL so I don't have to use these weird sorting functions)

Joel
This works with a little modification:

Code: Select all

static int wxCMPFUNC_CONV AClass::sortSegments(void *s, void *s1);
In this way it works!!

Thanks!!!!!

Post Reply