Sort with comparator
A vector (like an array) can be sorted using the
sort()
function. But this function sorts in ascending order by default. To sort an array in a different order, you need to use the so-called
comparator - a function that sets the sort order by comparing two objects.
Example
An example of a comparator that sorts the elements of an array in ascending order.
bool cmp(int first, int second)
{
return first < second;
}
and sorting the vector A
using the created comparator:
sort(A.begin(), A.end(), cmp);
Think about iterators