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
wxArrayInt::Sort - how to use it? Topic is solved
Hi,
Regards,
David
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] );
}
David
according to the documentation a function like this should be accepted :
this is just a guess... I tried it And it have worked...
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);
First of all, thank you very much for your quick replies!
DavidHart: your code is fine, thanks a lot!
But why isn't this documented in the manual?
Well, I tried that, too, but it didn't work. Strange...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...
DavidHart: your code is fine, thanks a lot!
But why isn't this documented in the manual?
I'm using this version:DavidHart wrote:In fact, looking at it again, it's longer than it need be. JustDavidHart: your code is fine, thanks a lot!
return *first > *second;
will work as well.
Code: Select all
return *first - *second;
DavidHart wrote:Well, it is, sort of, in wxArray, but wxArrayInt doesn't itself have a manual entry.But why isn't this documented in the manual?
Well, this isn't similar to your code at all, is it?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.
Regards,
outis
I tried it under mingw compiler and it worked fine...outis wrote:First of all, thank you very much for your quick replies!
Well, I tried that, too, but it didn't work. Strange...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...
DavidHart: your code is fine, thanks a lot!
But why isn't this documented in the manual?
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]);
}