38template <
typename ElementComparator>
39struct SortFunctionConverter
41 SortFunctionConverter (ElementComparator& e) : comparator (e) {}
43 template <
typename Type>
44 bool operator() (Type a, Type b) {
return comparator.compareElements (a, b) < 0; }
47 ElementComparator& comparator;
48 SortFunctionConverter& operator= (
const SortFunctionConverter&) =
delete;
82template <
class ElementType,
class ElementComparator>
83static void sortArray (ElementComparator& comparator,
84 ElementType*
const array,
87 const bool retainOrderOfEquivalentItems)
89 jassert (firstElement >= 0);
91 if (lastElement > firstElement)
93 SortFunctionConverter<ElementComparator> converter (comparator);
95 if (retainOrderOfEquivalentItems)
96 std::stable_sort (array + firstElement, array + lastElement + 1, converter);
98 std::sort (array + firstElement, array + lastElement + 1, converter);
127template <
class ElementType,
class ElementComparator>
128static int findInsertIndexInSortedArray (ElementComparator& comparator,
129 ElementType*
const array,
130 const ElementType newElement,
134 jassert (firstElement <= lastElement);
136 ignoreUnused (comparator);
139 while (firstElement < lastElement)
141 if (comparator.compareElements (newElement, array [firstElement]) == 0)
148 const int halfway = (firstElement + lastElement) >> 1;
150 if (halfway == firstElement)
152 if (comparator.compareElements (newElement, array [halfway]) >= 0)
157 else if (comparator.compareElements (newElement, array [halfway]) >= 0)
159 firstElement = halfway;
163 lastElement = halfway;
188template <
class ElementType>
192 using ParameterType =
typename TypeHelpers::ParameterType<ElementType>::type;
195 static int compareElements (ParameterType first, ParameterType second)
197 return (first < second) ? -1 : ((second < first) ? 1 : 0);
A simple ElementComparator class that can be used to sort an array of objects that support the '<' op...