About
maxon::BaseSort is a class template that allows to sort data in arrays and to search in sorted arrays. For a default implementation handling simple data types see maxon::SimpleSort.
- Note
- See also SortedArray.
Create
A new sorting class is created by implementing a custom class based on maxon::BaseSort. This custom class may implement:
LessThan
(): For sorting elements in an array.
IsEqual
(): For finding elements in an array.
Further flags that can be set are:
Sorting
An instance of the custom sorting class can be used to sort the elements stored in a maxon::BaseArray.
class IntegerSort :
public maxon::BaseSort<IntegerSort, maxon::BASESORTFLAGS::MOVEANDCOPYOBJECTS>
{
public:
{
return a < b;
}
};
FillWithRandomNumbers(numbers);
IntegerSort sort;
sort.Sort(numbers);
Searching
To find elements in the given array, the array must be sorted. The custom sorting class must implement both LessThan
() and IsEqual
().
{
public:
static inline Bool LessThan(CustomDate key, CustomDate element)
{
if (key.year > element.year)
return false;
if (key.year < element.year)
return true;
if (key.month > element.month)
return false;
if (key.month < element.month)
return true;
if (key.day >= element.day)
return false;
return true;
}
static inline Bool IsEqual(CustomDate key, CustomDate element)
{
if (key.year != element.year)
return false;
if (key.month != element.month)
return false;
if (key.day != element.day)
return false;
return true;
}
{
return key < element.year;
}
{
return key == element.year;
}
};
FillWithRandomDates(dates);
DateSort sort;
sort.Sort(dates);
const CustomDate* const date = sort.Find(2000, dates);
if (date)
if (index > 0)
CustomDate newDate;
newDate.year = 1970;
newDate.month = 1;
newDate.day = 1;
sort.FindInsertionIndex(newDate, dates, insertionIndex);
Further Reading