How to Sort a Vector Using a Custom Comparator in C++?
In C++, the std::sort() function sorts the given vector in increasing order by default. The custom comparator is a function that defines the order in which the elements of a std::vector should be sorted. It is passed as the parameter to the std::sort() function.
In this article, we will learn how to sort a vector using a custom comparator in C++.
Examples
Input: v = {5, 11, 9, 7, 4}
Output: 11 9 7 5 4
Explanation: Elements are sorted in the decreasing order.Input: v = {5, 9, 6, 4, 12, 3}
Output: 9 6 12 3 4 5
Explanation: Elements are sorted in the increasing order of the remainder after their division with 3.
In C++, the custom comparator function for std::sort() can by declared in three ways:
As Traditional Function
The simplest way to define the comparator is as traditional function that takes two parameters of required type and return a boolean value. It should return true if the first and second parameter are already in correct order, i.e. a is already before b. It returns false when they are not in the correct order.
Example
// C++ program to sort a vector by defining a
// custom comparator as traditional function
#include <bits/stdc++.h>
using namespace std;
// Comparator function
bool comp(int a, int b) {
// first parameter should be greater than
// second one (for decreasing order)
return a > b;
}
int main() {
vector<int> v = {5, 11, 9, 7, 4};
// Sort the elements of vector in decreasing
// order using comp
sort(v.begin(), v.end(), comp);
for (auto i : v)
cout << i << " ";
return 0;
}
Output
11 9 7 5 4
Time Complexity: O(n * log n), where n is the number of elements in the vector.
Auxiliary Space: O(log n)
As Lambda Expression
We can also define our custom comparator for std::sort() as lambda expression. It should have the same signature i.e. it should take two parameters and return true if they are in the correct order, false otherwise.
Example
// C++ program to sort a vector by defining custom
// comparator as lamda function
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {5, 11, 9, 7, 4};
// Sort the elements of vector in decreasing
// order
sort(v.begin(), v.end(), [](int a, int b) {
return a > b;
});
for (auto i : v)
cout << i << " ";
return 0;
}
Output
11 9 7 5 4
Time Complexity: O(n * log n), where n is the number of elements in the vector.
Auxiliary Space: O(log n)
As Function Object (Functor)
We can also define our custom comparator with std::sort() as function object (also known as "functor"). A functor is a class or struct that overloads the operator()
and acts like a function when used. The signature of the operator() function should satisfy the comparator requirement i.e. it should take two parameters and return a boolean value.
Example
// C++ program to sort a vector using custom
// comparator as function object
#include <bits/stdc++.h>
using namespace std;
// Custom comparator as a functor
struct Comp {
bool operator() (int a, int b) {
int ar = a % 3;
int br = b % 3;
return ar < br;
}
};
int main() {
vector<int> v = {5, 9, 6, 4, 12, 3};
// Sort elements of vector in increasing
// order of the remainders left after
// being divided by 3
sort(v.begin(), v.end(),Comp());
for (auto i : v)
cout << i << " ";
return 0;
}
Output
9 6 12 3 4 5
Time Complexity: O(n * log n), where n is the number of elements in the vector.
Auxiliary Space: O(log n)