VOOZH about

URL: https://www.geeksforgeeks.org/cpp/comparator-in-cpp/

⇱ Custom Comparator in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Custom Comparator in C++

Last Updated : 21 Aug, 2025

A custom comparator in C++ is a way to tell a container (like set or sort) how to compare and order elements differently than the default. Instead of using the usual "less than" (<), we can define our own rule.

For example, you can make it sort in descending order or by a specific property of a complex object. It's like giving the container our own sorting instructions.

How to Create a Comparator in C++?

In C++, we can create a comparator function using four methods. They are:

  1. Using Function Pointer
  2. Using Lambda Expression
  3. Using Functors

1. Comparator Using Function Pointer

In this method, we define a function that implements the comparison logic and returns some value. Then we use the pointer to this function as the comparator.

Example


Output
Sorted Vector: 1 2 3 4 5 7 

2. Comparator Using Lambda Expression

Lambda expressions can be used to declare the inline function definitions. We can also use the lambda expression to create a comparator just like we can do with function. Moreover, we can declare the lambda expression in a place where the comparator is required.

Example


Output
Sorted Vector: 1 2 3 4 7 8 

3. Comparator Using Functor (Function Object)

A functor (Function Object) is a class or struct that overloads the operator() such that it behaves as a function when called. We can also use such objects as a comparator by defining the comparison logic inside the () operator overloading function.

Example


Output
Sorted Vector: 1 2 3 4 6 9 

Application of Comparator in C++

In C++, comparator is widely used in many different cases. Some of the common application of comparators is as follows:

1. Sort the Characters in increasing frequency.

It can be used to count and sort characters in the string based on increasing frequency using a custom comparator.

Example

The below example demonstrates the use of comparator to sort the characters in increasing frequency.


Output
oooollleeH

2. Sort characters in increasing frequency and same frequency in decreasing alphabetical order.

It can be used to count and sort characters in the string based on increasing frequency and same frequency using a custom comparator.

Example


Output
oooollleeekhg 

3. Sort numbers by increasing set bit count

It is used to sort a vector of integers based on the number of set bits using a lambda function as the custom comparator.

Example


Output
1 8 2 5 17 13 

4. Segregate even odd numbers and even numbers first.

We can used comparator to sort a vector of integers to segregate even and odd numbers, placing even numbers before odd numbers.

Example


Output
after segregation: 2 4 6 1 3 5 7 

Advantages of custom comparators

  • Custom Sorting logic: We can define our own way to sort elements (e.g., descending, by name, by age).
  • Works with Complex data
  • Flexible with STL containers
  • Improves Code Reusability
Comment
Article Tags:
Article Tags: