How to Sort a Vector Using a Custom Comparator in C++?
Last Updated :
15 Oct, 2024
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++
// 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;
}
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++
// 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;
}
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++
// 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;
}
Time Complexity: O(n * log n), where n is the number of elements in the vector.
Auxiliary Space: O(log n)
Similar Reads
How to Use Custom Comparator with Set in C++? In C++, sets are associative containers that store unique elements in some sorted order. By default, set store data in increasing order but we can change this using a custom comparator. In this article, we will learn, how to declare a set with a custom comparator in C++ STL. Example Input: Data = {1
2 min read
How to Sort using Member Function as Comparator in C++? In C++, sorting is a common operation that can be customized by providing a comparator function. When working with classes, we might need to sort objects based on their member variables. Using a member function as a comparator is a good technique to achieve this.In this article, we will learn how to
3 min read
How to Sort a Vector of Custom Objects in C++? In C++, vectors are dynamic arrays and sorting a vector of custom objects means arranging the objects in a certain order. In this article, we will learn how to sort a vector of custom objects in C++. Sorting a Vector of Custom Objects in C++To sort a vector of custom objects in C++, we can use the s
2 min read
How to Initialize Multiset with Custom Comparator in C++? In C++, a multiset container stores the data in a sorted order. By default, this order is increasing order (using < operator as comparator) but we can change this order by providing a custom comparator. In this article, we will learn how to initialize a multiset with a custom comparator function
2 min read
How to Sort a Vector in a Map in C++? In C++, we can create a map container where the values associated with keys is a vector. In this article, we will learn how to sort a vector within a map in C++. Example Input: myMap = { {3, {9, 7, 3}}, {5, {4, 2, 8, 1, 6}}, {8, {1, 2, 5, 8}} }; Output: Map: Key: 3, Sorted Vector: [3 7 9 ] Key: 5, S
2 min read