Page 1 of 1

wxArrayInt::Sort - how to use it?

Posted: Sat Jan 27, 2007 10:58 am
by outis
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

Posted: Sat Jan 27, 2007 12:38 pm
by DavidHart
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

Posted: Sat Jan 27, 2007 12:45 pm
by manianis
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...

Posted: Sat Jan 27, 2007 1:24 pm
by outis
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?

Posted: Sat Jan 27, 2007 2:02 pm
by DavidHart
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.

Posted: Sat Jan 27, 2007 8:04 pm
by outis
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

Posted: Sat Jan 27, 2007 8:34 pm
by manianis
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