c++ How to sort an Array in JUCE using ElementComparator Class -
i have array , want sort array using arrayname.sort(elementcomparator& comparator) , defined in juce library, here link sort function : http://www.rawmaterialsoftware.com/juce/api/classarray.html#a13366b4435364fcd0d304cdcaebd726a
i not able understand how pass comparator sort function.
or, if can give me better way sort array of strings in ascending , descending order.
thanks lot
it pretty simple , described in documentation:
this use comparator object sort elements order. object passed must have method of form:
int compareelements (elementtype first, elementtype second);
..and method must return:
a value of < 0 if first comes before second
value of 0 if 2 objects equivalent
value of > 0 if second comes before first
so, sort array own comparator, have define class implements above stated method, example:
class myarraysorter { public: static int compareelements(elementtype a, elementtype b) { if (a < b) return -1; else if (a > b) return 1; else // if == b return 0; } };
of course, elementtype has type of element array holding , implementation of compareelements has adapted accordingly.
and pass array's sort method:
myarraysorter sorter; myarr.sort(sorter);
Comments
Post a Comment