wxArrayInt::Sort - how to use it? Topic is solved

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
Post Reply
outis
Earned a small fee
Earned a small fee
Posts: 22
Joined: Thu Nov 23, 2006 4:42 pm

wxArrayInt::Sort - how to use it?

Post by outis » Sat Jan 27, 2007 10:58 am

Hello,

someone here, who can tell me how I can use the Sort() function of wxArrayInt? I tried to find out by myself for hours, but neither the manual nor google could help me very much. :( The main problem is the compare-function. How should it look like?
It would be of great help to me, if someone could give me an example for such a function.

Regards,
outis

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

Post by DavidHart » Sat Jan 27, 2007 12:38 pm

Hi,

Code: Select all

static int wxCMPFUNC_CONV foosort(int *first, int *second)
{
if ( *first == *second ) return 0;
return *first > *second;
}

void foo()
{
wxArrayInt array;
for (int n=10; n > 0; --n ) array.Add( n-1 );
array.Sort( foosort );
for (size_t n=0; n < array.GetCount(); ++n ) wxLogDebug( wxT("%d "), array[n] );
}
Regards,

David

manianis
Experienced Solver
Experienced Solver
Posts: 72
Joined: Mon Jan 15, 2007 11:00 am

Post by manianis » Sat Jan 27, 2007 12:45 pm

according to the documentation a function like this should be accepted :

Code: Select all

int compare_int(int *a, int *b)
{
  if (*a > *b) return 1;
  else if (*a < *b) return -1;
  else return 0;
}

...

wxArrayInt arr;

fill_array(arr);
arr.Sort(compare_int);
this is just a guess... I tried it And it have worked...

outis
Earned a small fee
Earned a small fee
Posts: 22
Joined: Thu Nov 23, 2006 4:42 pm

Post by outis » Sat Jan 27, 2007 1:24 pm

First of all, thank you very much for your quick replies!
manianis wrote:according to the documentation a function like this should be accepted : [...]
this is just a guess... I tried it And it have worked...
Well, I tried that, too, but it didn't work. Strange...

DavidHart: your code is fine, thanks a lot! :)
But why isn't this documented in the manual?

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

Post by DavidHart » Sat Jan 27, 2007 2:02 pm

DavidHart: your code is fine, thanks a lot!
In fact, looking at it again, it's longer than it need be. Just
return *first > *second;
will work as well.
But why isn't this documented in the manual?
Well, it is, sort of, in wxArray, but wxArrayInt doesn't itself have a manual entry.

outis
Earned a small fee
Earned a small fee
Posts: 22
Joined: Thu Nov 23, 2006 4:42 pm

Post by outis » Sat Jan 27, 2007 8:04 pm

DavidHart wrote:
DavidHart: your code is fine, thanks a lot!
In fact, looking at it again, it's longer than it need be. Just
return *first > *second;
will work as well.
I'm using this version:

Code: Select all

return *first - *second;
But your code is still fine. What can I do with a very simple, very short compare function, if it doesn't work? 8)
DavidHart wrote:
But why isn't this documented in the manual?
Well, it is, sort of, in wxArray, but wxArrayInt doesn't itself have a manual entry.
manual wrote: wxArray::Sort
void Sort(CMPFUNC<T> compareFunction)

The notation CMPFUNC<T> should be read as if we had the following declaration:


template int CMPFUNC(T *first, T *second);

where T is the type of the array elements. I.e. it is a function returning int which is passed two arguments of type T *.
Sorts the array using the specified compare function: this function should return a negative, zero or positive value according to whether the first element passed to it is less than, equal to or greater than the second one.

wxSortedArray doesn't have this function because it is always sorted.
Well, this isn't similar to your code at all, is it?

Regards,
outis

manianis
Experienced Solver
Experienced Solver
Posts: 72
Joined: Mon Jan 15, 2007 11:00 am

Post by manianis » Sat Jan 27, 2007 8:34 pm

outis wrote:First of all, thank you very much for your quick replies!
manianis wrote:according to the documentation a function like this should be accepted : [...]
this is just a guess... I tried it And it have worked...
Well, I tried that, too, but it didn't work. Strange...

DavidHart: your code is fine, thanks a lot! :)
But why isn't this documented in the manual?
I tried it under mingw compiler and it worked fine...

Code: Select all

int compare_int(int *a, int *b)
{
  if (*a > *b) return 1;
  else if (*a < *b) return -1;
  else return 0;
}

void MyFrame::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));

    wxArrayInt arr;
    arr.Add(4);
    arr.Add(1);
    arr.Add(5);
    arr.Add(4);
    arr.Add(1);
    arr.Add(5);

    arr.Sort(compare_int);
    for (int i = 0 ; i < arr.GetCount() ; i++)
        printf("%d\n", arr[i]);
}
the printf show : 1, 1, 4, 4, 5, 5

Post Reply