C++ program to demonstrate sort() with any compare function

Sorting is required in most of the programs in competitive programming, In this tutorial, we will see how to easily use the sort()  method which is provided by the C++ Library.

Regular Sort
We can easily sort a container of elements using the regular sort function, which by default sorts the elements in the container in ascending order. So no compare function is needed for this kind of sort.

Code:

#include <bits/stdc++.h>

using namespace std;


int main() {
    
    int arr[5]={4,3,6,1,2};
    cout<<"Before sorting elements:"<<endl;
    for(int i=0;i<5;i++){
        cout<<arr[i]<<" ";
    }
    cout<<endl;
    
    sort(arr,arr+5); 
    
    cout<<"After sorting elements:"<<endl;
    for(int i=0;i<5;i++){
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}

Output

Before sorting elements:
4 3 6 1 2 
After sorting elements:
1 2 3 4 6 

Sorting in descending order
To sort the elements in a container in descending order, we need to pass a compare function to the sort method, the syntax is the same as before except for an additional parameter.
The compare function we use here is greater<int>() which sorts our vector/container in descending order.

Code:

#include <bits/stdc++.h>

using namespace std;


int main() {
    
    int arr[5]={4,3,6,1,2};
    cout<<"Before sorting elements:"<<endl;
    for(int i=0;i<5;i++){
        cout<<arr[i]<<" ";
    }
    cout<<endl;
    
    sort(arr,arr+5,greater<int>());
    
    cout<<"After sorting elements:"<<endl;
    for(int i=0;i<5;i++){
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}

Output

Before sorting elements:
4 3 6 1 2 
After sorting elements:
6 4 3 2 1 

Using custom compare function
Let`s say we have an array of type pair<int, int> If we use the regular sort() method on it, the array will get sorted based on the first value of the pair, but if we need to sort it based on the second value of the pair we have to build a custom compare function ourselves and use it.
Doing this is pretty easy as well.
It is done as follows

Code:

#include <bits/stdc++.h>

using namespace std;
 
//custom sort function which
//takes 2 pairs and returns true
//If the second value of the first pair
//is less than the second value of the 
//second pair

bool customSort(pair<int,int> a,pair<int,int> b){
    return a.second<b.second;
}

int main() {
    
   
    pair<int,int> vec[4]={{6,8}, {1,9}, {2,4}, {4,7}}; ;
    
    cout<<"Before sorting the array is:"<<endl;
    for(int i=0;i<4;i++){
        cout<<vec[i].first<<" "<<vec[i].second<<endl;
    }
    cout<<endl;

    sort(vec,vec+4,customSort);

    cout<<"After performing custom sort of the array based on second value"<<endl;
    for(int i=0;i<4;i++){
        cout<<vec[i].first<<" "<<vec[i].second<<endl;
    }
}

Output

Before sorting the array is:
6 8
1 9
2 4
4 7

After performing custom sort of the array based on second value
2 4
4 7
6 8
1 9

Leave a Reply

Your email address will not be published. Required fields are marked *