Skip to content
Related Articles
Open in App
Not now

Related Articles

Sorting 2D Vector of Pairs in C++

Improve Article
Save Article
  • Last Updated : 12 Jul, 2022
Improve Article
Save Article

A 2D vector also known as vector of vectors is a vector in which each element is a vector on its own. In other words, It is a matrix implemented with the help of vectors.

What is a 2D vector of pairs?

A 2D vector of pairs is a vector in which each element is a vector of pairs on its own. In other words, It is a matrix implemented with the help of vectors of pairs.

Example: Below is the C++ program to implement a 2D vector of pairs.

C++




// C++ program to demonstrate the
// working of vector of vectors
// of pairs
#include <bits/stdc++.h>
using namespace std;
 
// Function to print 2D vector elements
void print(vector<vector<pair<int, int> > >& myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer)
    {
        // Each element of the 2D vector is
        // a vector itself
        vector<pair<int, int> > myVector =
                                currentVector;
 
        // Iterating over the vector
        // elements
        cout << "[  ";
        for (auto pr : myVector)
         {
            // Print the element
            cout << "{";
            cout << pr.first << ", " <<
                    pr.second;
            cout << "}  ";
        }
        cout << "]\n";
    }
}
 
// Driver code
int main()
{
    // Declaring a 2D vector of pairs
    // Pairs are of type {char, bool}
    vector<vector<pair<int, int> > > myContainer;
 
    // Initializing vectors of pairs
    vector<pair<int, int> > vect1 =
    {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
    vector<pair<int, int> > vect2 =
    {{9, 10}, {11, 12}, {13, 14}, {15, 16}};
    vector<pair<int, int> > vect3 =
    {{17, 18}, {19, 20}, {21, 22}, {23, 24}};
    vector<pair<int, int> > vect4 =
    {{25, 26}, {27, 28}, {29, 30}, {31, 32}};
 
    // Inserting vectors in the 2D vector
    myContainer.push_back(vect1);
    myContainer.push_back(vect2);
    myContainer.push_back(vect3);
    myContainer.push_back(vect4);
 
    print(myContainer);
 
    return 0;
}


Output[ {1, 2} {3, 4} {5, 6} {7, 8} ] [ {9, 10} {11, 12} {13, 14} {15, 16} ] [ {17, 18} {19, 20} {21, 22} {23, 24} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ]

This article focuses on discussing different techniques used to sort a 2D vector of pairs.

Case 1: To sort a particular row of a 2D vector:

  •  On the basis of the first values of pairs: This type of sorting arranges a selected row of a 2D vector in ascending order of the first value of pairs. This is achieved by using “sort()” and passing iterators of 1D vector as its arguments.

Example:

Input:
(4, 1)   (1, 9)   (7, 2)
(3, 2)   (4, 5)   (8, 1)
(1, 6)   (3, 2)   (1, 4)                     

Output:
(1, 9)   (4, 1)   (7, 2)  
(3, 2)   (4, 5)   (8, 1)
(1, 6)   (3, 2)   (1, 4)

Below is the implementation of the above approach:

C++




// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs
#include <bits/stdc++.h>
using namespace std;
 
// Function to print 2D vector elements
void print(vector<vector<pair<int, int> > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer)
    {
        // Each element of the 2D vector is
        // a vector itself
        vector<pair<int, int> > myVector =
                                currentVector;
 
        // Iterating over the vector
        // elements
        cout << "[  ";
        for (auto pr : myVector)
        {
            // Print the element
            cout << "{";
            cout << pr.first << ", " <<
                    pr.second;
            cout << "}  ";
        }
        cout << "]\n";
    }
}
 
// Driver code
int main()
{
    // Declaring a 2D vector of pairs
    // Pairs are of type {char, bool}
    vector<vector<pair<int, int> > > myContainer;
 
    // Initializing vectors of pairs
    vector<pair<int, int> > vect1 =
    {{2, 1}, {1, 9}, {4, 3}, {8, 1}};
    vector<pair<int, int> > vect2 =
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}};
    vector<pair<int, int> > vect3 =
    {{17, 18}, {19, 20}, {21, 22}, {23, 24}};
    vector<pair<int, int> > vect4 =
    {{25, 26}, {27, 28}, {29, 30}, {31, 32}};
 
    // Inserting vectors in the 2D vector
    myContainer.push_back(vect1);
    myContainer.push_back(vect2);
    myContainer.push_back(vect3);
    myContainer.push_back(vect4);
 
    // Print the vector elements
    // before sorting
    cout << "Before sorting, ";
    print(myContainer);
 
    // Sorting the first row of 2D vector
    // of pairs on the basis of first
    // element of pairs
    sort(myContainer[0].begin(),
         myContainer[0].end());
 
    cout << "\n\n After sorting the first row " <<
            "on the basis of first element of pairs, ";
    print(myContainer);
 
    return 0;
}


OutputBefore sorting, myContainer elements: [ {2, 1} {1, 9} {4, 3} {8, 1} ] [ {19, 10} {11, 2} {12, 14} {14, 6} ] [ {17, 18} {19, 20} {21, 22} {23, 24} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ] After sorting the first row on the basis of first element of pairs, myContainer elements: [ {1, 9} {2, 1} {4, 3} {8, 1} ] [ {19, 10} {11, 2} {12, 14} {14, 6} ] [ {17, 18} {19, 20} {21, 22} {23, 24} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ]
  • On the basis of the second value of pairs: This type of sorting arranges a selected row of a 2D vector in ascending order of the second value of the pair. This is achieved by using “sort()” and passing iterators of 1D vector as its arguments. In this case, there is a need to pass a custom comparator as a third argument that implements the logic to sort on the basis of the second value of pairs.

Example:

Input:
(4, 1)   (1, 9)   (7, 2)
(3, 2)   (4, 5)   (8, 1)
(1, 6)   (3, 2)   (1, 4)                     

Output:
(4, 1)   (7, 2)   (1, 9)  
(3, 2)   (4, 5)   (8, 1)
(1, 6)   (3, 2)   (1, 4)

 Below is the implementation of the above approach:

C++




// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs
#include <bits/stdc++.h>
using namespace std;
 
// Custom comparator function
bool myComparator(pair<int, int>& pair1,
                  pair<int, int>& pair2)
{
    return pair1.second < pair2.second;
}
 
// Function to print 2D vector elements
void print(vector<vector<pair<int, int> > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer)
    {
        // Each element of the 2D vector is
        // a vector itself
        vector<pair<int, int> > myVector =
                                currentVector;
 
        // Iterating over the vector
        // elements
        cout << "[  ";
        for (auto pr : myVector)
        {
            // Print the element
            cout << "{";
            cout << pr.first << ", " <<
                    pr.second;
            cout << "}  ";
        }
        cout << "]\n";
    }
}
 
// Driver code
int main()
{
    // Declaring a 2D vector of pairs
    // Pairs are of type {char, bool}
    vector<vector<pair<int, int> > > myContainer;
 
    // Initializing vectors of pairs
    vector<pair<int, int> > vect1 =
    {{2, 1}, {1, 9}, {4, 3}, {8, 1}};
    vector<pair<int, int> > vect2 =
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}};
    vector<pair<int, int> > vect3 =
    {{17, 18}, {19, 20}, {21, 22}, {23, 24}};
    vector<pair<int, int> > vect4 =
    {{25, 26}, {27, 28}, {29, 30}, {31, 32}};
 
    // Inserting vectors in the 2D vector
    myContainer.push_back(vect1);
    myContainer.push_back(vect2);
    myContainer.push_back(vect3);
    myContainer.push_back(vect4);
 
    // Print the vector elements before sorting
    cout << "Before sorting, ";
    print(myContainer);
 
    // Sorting first row of 2D vector of pairs
    // on the basis of second element of pairs
    // By passing a custom comparator as a
    // third argument
    sort(myContainer[0].begin(),
         myContainer[0].end(), myComparator);
 
    cout << "\n\n After sorting the first row on " <<
            "the basis of second element of pairs, ";
    print(myContainer);
 
    return 0;
}


OutputBefore sorting, myContainer elements: [ {2, 1} {1, 9} {4, 3} {8, 1} ] [ {19, 10} {11, 2} {12, 14} {14, 6} ] [ {17, 18} {19, 20} {21, 22} {23, 24} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ] After sorting the first row on the basis of second element of pairs, myContainer elements: [ {2, 1} {8, 1} {4, 3} {1, 9} ] [ {19, 10} {11, 2} {12, 14} {14, 6} ] [ {17, 18} {19, 20} {21, 22} {23, 24} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ]

Case 2: To sort the entire 2D vector on basis of a particular column:

  • On the basis of the first values of pairs: In this type of sorting 2D vector is entirely sorted on basis of a chosen column. For example, if the chosen column is second, the row with the smallest first value of pairs of the second column becomes the first row, the second smallest first value of pairs in the second column becomes the second row, and so on.

Example:

Input:
(4, 1)   (1, 9)   (7, 2)
(3, 2)   (4, 5)   (8, 1)
(1, 6)   (3, 2)   (1, 4)                     

Output:
(4, 1)   (1, 9)   (7, 2)  
(1, 6)   (3, 2)   (1, 4)    
(3, 2)   (4, 5)   (8, 1)  

Below is the C++ program to implement the above approach:

C++




// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs
#include <bits/stdc++.h>
using namespace std;
 
// Custom comparator function
bool myComparator(vector<pair<int, int> >& vector1,
                  vector<pair<int, int> >& vector2)
{
    return vector1[1].first < vector2[1].first;
}
 
// Function to print 2D vector elements
void print(vector<vector<pair<int, int> > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer)
    {
        // Each element of the 2D vector is
        // a vector itself
        vector<pair<int, int> > myVector =
                                currentVector;
 
        // Iterating over the vector
        // elements
        cout << "[  ";
        for (auto pr : myVector)
        {
            // Print the element
            cout << "{";
            cout << pr.first << ", " <<
                    pr.second;
            cout << "}  ";
        }
        cout << "]\n";
    }
}
 
// Driver code
int main()
{
    // Declaring a 2D vector of pairs
    // Pairs are of type {char, bool}
    vector<vector<pair<int, int> > > myContainer;
 
    // Initializing vectors of pairs
    vector<pair<int, int> > vect1 =
    {{3, 5}, {10, 3}, {2, 1}, {3, 8}};
    vector<pair<int, int> > vect2 =
    {{1, 6}, {9, 1}, {2, 5}, {1, 5}};
    vector<pair<int, int> > vect3 =
    {{1, 5}, {2, 5}, {12, 12}, {1, 4}};
    vector<pair<int, int> > vect4 =
    {{5, 2}, {1, 52}, {9, 3}, {3, 32}};
 
    // Inserting vectors in the 2D vector
    myContainer.push_back(vect1);
    myContainer.push_back(vect2);
    myContainer.push_back(vect3);
    myContainer.push_back(vect4);
 
    // Print the vector elements
    // before sorting
    cout << "Before sorting, ";
    print(myContainer);
 
    // Sorting 2D vector of pairs on the basis
    // of first element of pairs of the second
    // column by passing a custom comparator
    // as a third argument
    sort(myContainer.begin(), myContainer.end(),
         myComparator);
 
    cout << "\n\n After sorting the 2D vector on the " <<
            "basis of first element of pairs of the second column, ";
    print(myContainer);
 
    return 0;
}


OutputBefore sorting, myContainer elements: [ {3, 5} {10, 3} {2, 1} {3, 8} ] [ {1, 6} {9, 1} {2, 5} {1, 5} ] [ {1, 5} {2, 5} {12, 12} {1, 4} ] [ {5, 2} {1, 52} {9, 3} {3, 32} ] After sorting the 2D vector on the basis of first element of pairs of the second column, myContainer elements: [ {5, 2} {1, 52} {9, 3} {3, 32} ] [ {1, 5} {2, 5} {12, 12} {1, 4} ] [ {1, 6} {9, 1} {2, 5} {1, 5} ] [ {3, 5} {10, 3} {2, 1} {3, 8} ]
  •  On the basis of the second values of pairs: In this type of sorting 2D vector is entirely sorted on basis of a chosen column. For example, if the chosen column is second, the row with the smallest second value of pairs of the second column becomes the first row, the second smallest second value of pairs in the second column becomes the second row, and so on.

For example, sorting the entire 2D vector on the basis of the second value of pairs of the second column would result into,

Input:
(4, 1)   (1, 9)   (7, 2)
(3, 2)   (4, 5)   (8, 1)
(1, 6)   (3, 2)   (1, 4)                      

Output:  
(1, 6)   (3, 2)   (1, 4) 
(3, 2)   (4, 5)   (8, 1)  
(4, 1)   (1, 9)   (7, 2) 

Below is the C++ program to implement the above approach:

C++




// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs
#include <bits/stdc++.h>
using namespace std;
 
// Custom comparator function
bool myComparator(vector<pair<int, int> >& vector1,
                  vector<pair<int, int> >& vector2)
{
    return vector1[1].second < vector2[1].second;
}
 
// Function to print 2D vector elements
void print(vector<vector<pair<int, int> > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer)
    {
        // Each element of the 2D vector is
        // a vector itself
        vector<pair<int, int> > myVector =
                                currentVector;
 
        // Iterating over the vector
        // elements
        cout << "[  ";
        for (auto pr : myVector)
        {
            // Print the element
            cout << "{";
            cout << pr.first << ", " <<
                    pr.second;
            cout << "}  ";
        }
        cout << "]\n";
    }
}
 
// Driver code
int main()
{
    // Declaring a 2D vector of pairs
    // Pairs are of type {char, bool}
    vector<vector<pair<int, int> > > myContainer;
 
    // Initializing vectors of pairs
    vector<pair<int, int> > vect1 =
    {{3, 5}, {10, 3}, {2, 1}, {3, 8}};
    vector<pair<int, int> > vect2 =
    {{1, 6}, {9, 1}, {2, 5}, {1, 5}};
    vector<pair<int, int> > vect3 =
    {{1, 5}, {2, 5}, {12, 12}, {1, 4}};
    vector<pair<int, int> > vect4 =
    {{5, 2}, {1, 52}, {9, 3}, {3, 32}};
 
    // Inserting vectors in the 2D vector
    myContainer.push_back(vect1);
    myContainer.push_back(vect2);
    myContainer.push_back(vect3);
    myContainer.push_back(vect4);
 
    // Print the vector elements before sorting
    cout << "Before sorting, ";
    print(myContainer);
 
    // Sorting 2D vector of pairs on the basis of
    // first element of pairs of the second column
    // By passing a custom comparator as a third argument
    sort(myContainer.begin(), myContainer.end(),
         myComparator);
 
    cout << "\n\n After sorting the 2D vector on " <<
            "the basis of first element of pairs of " <<
            "the second column, ";
    print(myContainer);
 
    return 0;
}


OutputBefore sorting, myContainer elements: [ {3, 5} {10, 3} {2, 1} {3, 8} ] [ {1, 6} {9, 1} {2, 5} {1, 5} ] [ {1, 5} {2, 5} {12, 12} {1, 4} ] [ {5, 2} {1, 52} {9, 3} {3, 32} ] After sorting the 2D vector on the basis of first element of pairs of the second column, myContainer elements: [ {1, 6} {9, 1} {2, 5} {1, 5} ] [ {3, 5} {10, 3} {2, 1} {3, 8} ] [ {1, 5} {2, 5} {12, 12} {1, 4} ] [ {5, 2} {1, 52} {9, 3} {3, 32} ]

Case 3: To sort a particular row of a 2D vector of pairs in descending order

  • On the basis of the first values of pairs: This type of sorting arranges a selected row of a 2D vector in descending order by the first element of pairs. This is achieved by using “sort()” and passing iterators of 1D vector as its arguments. In this case, there is a need to pass a custom comparator as a third argument that implements the logic to sort on the basis of the first element of pairs.

Example:

Input:
(4, 1)   (1, 9)   (7, 2)
(3, 2)   (4, 5)   (8, 1)
(1, 6)   (3, 2)   (1, 4)                     

Output:
(7, 2)   (4, 1)   (1, 9)  
(3, 2)   (4, 5)   (8, 1)
(1, 6)   (3, 2)   (1, 4)

Below is the C++ program to implement the above approach:

C++




// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs in descending order
#include <bits/stdc++.h>
using namespace std;
 
// Custom comparator function
bool myComparator(pair<int, int>& pair1,
                  pair<int, int>& pair2)
{
    return pair1.first > pair2.first;
}
 
// Function to print 2D vector elements
void print(vector<vector<pair<int, int> > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer)
    {
        // Each element of the 2D vector is
        // a vector itself
        vector<pair<int, int> > myVector =
                                currentVector;
 
        // Iterating over the vector
        // elements
        cout << "[  ";
        for (auto pr : myVector)
        {
            // Print the element
            cout << "{";
            cout << pr.first << ", " <<
                    pr.second;
            cout << "}  ";
        }
        cout << "]\n";
    }
}
 
// Driver code
int main()
{
    // Declaring a 2D vector of pairs
    // Pairs are of type {char, bool}
    vector<vector<pair<int, int> > > myContainer;
 
    // Initializing vectors of pairs
    vector<pair<int, int> > vect1 =
    {{2, 1}, {1, 9}, {4, 3}, {8, 1}};
    vector<pair<int, int> > vect2 =
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}};
    vector<pair<int, int> > vect3 =
    {{17, 18}, {19, 20}, {21, 22}, {23, 24}};
    vector<pair<int, int> > vect4 =
    {{25, 26}, {27, 28}, {29, 30}, {31, 32}};
 
    // Inserting vectors in the 2D vector
    myContainer.push_back(vect1);
    myContainer.push_back(vect2);
    myContainer.push_back(vect3);
    myContainer.push_back(vect4);
 
    // Print the vector elements
    // before sorting
    cout << "Before sorting, ";
    print(myContainer);
 
    // Sorting the first row of 2D vector
    // of pairs in descending order
    // of the first element of pairs
    sort(myContainer[0].begin(),
         myContainer[0].end(), myComparator);
 
    cout << "\n\n After sorting the first row " <<
            "in descending order of the first " <<
            "element of pairs, ";
    print(myContainer);
 
    return 0;
}


OutputBefore sorting, myContainer elements: [ {2, 1} {1, 9} {4, 3} {8, 1} ] [ {19, 10} {11, 2} {12, 14} {14, 6} ] [ {17, 18} {19, 20} {21, 22} {23, 24} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ] After sorting the first row in descending order of the first element of pairs, myContainer elements: [ {8, 1} {4, 3} {2, 1} {1, 9} ] [ {19, 10} {11, 2} {12, 14} {14, 6} ] [ {17, 18} {19, 20} {21, 22} {23, 24} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ]
  • On the basis of the second values of pairs: This type of sorting arranges a selected row of a 2D vector in descending order by the second element of pairs. This is achieved by using “sort()” and passing iterators of 1D vector as its arguments. In this case, there is a need to pass a custom comparator as a third argument that implements the logic to sort in descending order on the basis of the second element of pairs.

Example:

Input:
(4, 1)   (1, 9)   (7, 2)
(3, 2)   (4, 5)   (8, 1)              
(1, 6)   (3, 2)   (1, 4)                     

Output:
(1, 9)   (7, 2)   (4, 1)  
(3, 2)   (4, 5)   (8, 1)
(1, 6)   (3, 2)   (1, 4)

Below is the C++ program to implement the above approach:

C++




// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs in descending order
#include <bits/stdc++.h>
using namespace std;
 
// Custom comparator function
bool myComparator(pair<int, int>& pair1,
           pair<int, int>& pair2)
{
    return pair1.second > pair2.second;
}
 
// Function to print 2D vector elements
void print(vector<vector<pair<int, int> > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer)
    {
        // Each element of the 2D vector is
        // a vector itself
        vector<pair<int, int> > myVector =
                                currentVector;
 
        // Iterating over the vector
        // elements
        cout << "[  ";
        for (auto pr : myVector)
        {
            // Print the element
            cout << "{";
            cout << pr.first << ", " <<
                    pr.second;
            cout << "}  ";
        }
        cout << "]\n";
    }
}
 
// Driver code
int main()
{
    // Declaring a 2D vector of pairs
    // Pairs are of type {char, bool}
    vector<vector<pair<int, int> > > myContainer;
 
    // Initializing vectors of pairs
    vector<pair<int, int> > vect1 =
    {{2, 1}, {1, 9}, {4, 3}, {8, 1}};
    vector<pair<int, int> > vect2 =
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}};
    vector<pair<int, int> > vect3 =
    {{17, 18}, {19, 20}, {21, 22}, {23, 24}};
    vector<pair<int, int> > vect4 =
    {{25, 26}, {27, 28}, {29, 30}, {31, 32}};
 
    // Inserting vectors in the 2D vector
    myContainer.push_back(vect1);
    myContainer.push_back(vect2);
    myContainer.push_back(vect3);
    myContainer.push_back(vect4);
 
    // Print the vector elements before sorting
    cout << "Before sorting, ";
    print(myContainer);
 
    // Sorting first row of 2D vector of pairs
    // in descending order of second element of pairs
    // By passing a custom comparator as a third argument
    sort(myContainer[0].begin(),
         myContainer[0].end(), myComparator);
 
    cout << "\n\n After sorting the first row " <<
            "in descending order of second element " <<
            "of pairs, ";
    print(myContainer);
 
    return 0;
}


OutputBefore sorting, myContainer elements: [ {2, 1} {1, 9} {4, 3} {8, 1} ] [ {19, 10} {11, 2} {12, 14} {14, 6} ] [ {17, 18} {19, 20} {21, 22} {23, 24} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ] After sorting the first row in descending order of second element of pairs, myContainer elements: [ {1, 9} {4, 3} {2, 1} {8, 1} ] [ {19, 10} {11, 2} {12, 14} {14, 6} ] [ {17, 18} {19, 20} {21, 22} {23, 24} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ]

Case 4: To sort the entire 2D vector on basis of a particular column in descending order:

  • On the basis of the first values of pairs: In this type of sorting, the 2D vector is entirely sorted on basis of the first value of pairs of the chosen column in descending order. For example, if the chosen column is second, the row with the greatest value of the first element of pairs in the second column becomes the first row, the second greatest value of the second element of pairs in the second column becomes the second row, and so on. 
    In this case, there is a need to pass a custom comparator as a third argument that implements the logic to sort in descending order on the basis of the first element of pairs.

Example:

Input:
(4, 1)   (1, 9)   (7, 2)
(3, 2)   (4, 5)   (8, 1)
(1, 6)   (3, 2)   (1, 4)                      

Output:
(3, 2)   (4, 5)   (8, 1)
(1, 6)   (3, 2)   (1, 4)  
(4, 1)   (1, 9)   (7, 2)

Below is the C++ program to implement the above approach:

C++




// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs in descending order
#include <bits/stdc++.h>
using namespace std;
 
// Custom comparator function
bool myComparator(vector<pair<int, int> >& vector1,
                  vector<pair<int, int> >& vector2)
{
    return vector1[1].first > vector2[1].first;
}
 
// Function to print 2D vector elements
void print(vector<vector<pair<int, int> > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer)
    {
        // Each element of the 2D vector is
        // a vector itself
        vector<pair<int, int> > myVector =
                                currentVector;
 
        // Iterating over the vector
        // elements
        cout << "[  ";
        for (auto pr : myVector)
        {
            // Print the element
            cout << "{";
            cout << pr.first << ", " <<
                    pr.second;
            cout << "}  ";
        }
        cout << "]\n";
    }
}
 
// Driver code
int main()
{
    // Declaring a 2D vector of pairs
    // Pairs are of type {char, bool}
    vector<vector<pair<int, int> > > myContainer;
 
    // Initializing vectors of pairs
    vector<pair<int, int> > vect1 =
    {{3, 5}, {10, 3}, {2, 1}, {3, 8}};
    vector<pair<int, int> > vect2 =
    {{1, 6}, {9, 1}, {2, 5}, {1, 5}};
    vector<pair<int, int> > vect3 =
    {{1, 5}, {2, 5}, {12, 12}, {1, 4}};
    vector<pair<int, int> > vect4 =
    {{5, 2}, {1, 52}, {9, 3}, {3, 32}};
 
    // Inserting vectors in the 2D vector
    myContainer.push_back(vect1);
    myContainer.push_back(vect2);
    myContainer.push_back(vect3);
    myContainer.push_back(vect4);
 
    // Print the vector elements before sorting
    cout << "Before sorting, ";
    print(myContainer);
 
    // Sorting 2D vector of pairs in descending
    // order of the first element of pairs of
    // the second column by passing a custom
    // comparator as a third argument
    sort(myContainer.begin(),
         myContainer.end(), myComparator);
 
    cout << "\n\n After sorting the 2D vector in " <<
            "descending order of the first element " <<
            "of pairs of the second column, ";
    print(myContainer);
 
    return 0;
}


OutputBefore sorting, myContainer elements: [ {3, 5} {10, 3} {2, 1} {3, 8} ] [ {1, 6} {9, 1} {2, 5} {1, 5} ] [ {1, 5} {2, 5} {12, 12} {1, 4} ] [ {5, 2} {1, 52} {9, 3} {3, 32} ] After sorting the 2D vector in descending order of the first element of pairs of the second column, myContainer elements: [ {3, 5} {10, 3} {2, 1} {3, 8} ] [ {1, 6} {9, 1} {2, 5} {1, 5} ] [ {1, 5} {2, 5} {12, 12} {1, 4} ] [ {5, 2} {1, 52} {9, 3} {3, 32} ]
  • On the basis of the second values of pairs: In this type of sorting, the 2D vector is entirely sorted on basis of the second element of pairs of the chosen column in descending order. For example, if the chosen column is second, the row with the greatest value of the second element of pairs in the second column becomes the first row, the second greatest value of the second element of pairs in the second column becomes the second row, and so on. 
    In this case, there is a need to pass a custom comparator as a third argument that implements the logic to sort in descending order on the basis of the second element of pairs.

Example:

Input:
(4, 1)   (1, 9)   (7, 2)                      
(3, 2)   (4, 2)   (8, 1)      
(1, 6)   (3, 5)   (1, 4)                     

Output:
(4, 1)   (1, 9)   (7, 2)        
(1, 6)   (3, 5)   (1, 4)   
(3, 2)   (4, 2)   (8, 1) 

Below is the C++ program to implement the above approach:

C++




// C++ program to demonstrate the
// working of sorting vector of vectors
// of pairs in descending order
#include <bits/stdc++.h>
using namespace std;
 
// Custom comparator function
bool myComparator(vector<pair<int, int> >& vector1,
                  vector<pair<int, int> >& vector2)
{
    return vector1[1].second > vector2[1].second;
}
 
// Function to print 2D vector elements
void print(vector<vector<pair<int, int> > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer)
    {
        // Each element of the 2D vector is
        // a vector itself
        vector<pair<int, int> > myVector =
                                currentVector;
 
        // Iterating over the vector
        // elements
        cout << "[  ";
        for (auto pr : myVector)
        {
            // Print the element
            cout << "{";
            cout << pr.first << ", " <<
                    pr.second;
            cout << "}  ";
        }
        cout << "]\n";
    }
}
 
// Driver code
int main()
{
    // Declaring a 2D vector of pairs
    // Pairs are of type {char, bool}
    vector<vector<pair<int, int> > > myContainer;
 
    // Initializing vectors of pairs
    vector<pair<int, int> > vect1 =
    {{3, 5}, {10, 3}, {2, 1}, {3, 8}};
    vector<pair<int, int> > vect2 =
    {{1, 6}, {9, 1}, {2, 5}, {1, 5}};
    vector<pair<int, int> > vect3 =
    {{1, 5}, {2, 5}, {12, 12}, {1, 4}};
    vector<pair<int, int> > vect4 =
    {{5, 2}, {1, 52}, {9, 3}, {3, 32}};
 
    // Inserting vectors in the 2D vector
    myContainer.push_back(vect1);
    myContainer.push_back(vect2);
    myContainer.push_back(vect3);
    myContainer.push_back(vect4);
 
    // Print the vector elements before sorting
    cout << "Before sorting, ";
    print(myContainer);
 
    // Sorting 2D vector of pairs in descending
    // order of second element of pairs of the
    // second column by passing a custom comparator
    // as a third argument
    sort(myContainer.begin(),
         myContainer.end(), myComparator);
 
    cout << "\n\n After sorting the 2D vector " <<
            "in descending order of second element " <<
            "of pairs of the second column, ";
    print(myContainer);
 
    return 0;
}


OutputBefore sorting, myContainer elements: [ {3, 5} {10, 3} {2, 1} {3, 8} ] [ {1, 6} {9, 1} {2, 5} {1, 5} ] [ {1, 5} {2, 5} {12, 12} {1, 4} ] [ {5, 2} {1, 52} {9, 3} {3, 32} ] After sorting the 2D vector in descending order of second element of pairs of the second column, myContainer elements: [ {5, 2} {1, 52} {9, 3} {3, 32} ] [ {1, 5} {2, 5} {12, 12} {1, 4} ] [ {3, 5} {10, 3} {2, 1} {3, 8} ] [ {1, 6} {9, 1} {2, 5} {1, 5} ]

Case 5: Sorting a 2D Vector of pairs on the basis of the number of columns in a row in ascending order:

In this type of sorting, a 2D vector of pairs is sorted on basis of a number of the column in ascending order. This is achieved by passing a third argument, a custom comparator function to the “sort()” method.

Example:

Input:
(4, 1)   (1, 9)   (7, 2)
(8, 11)
(1, 6)   (3, 2)                     

Output:
(8, 11) 
(1, 6)   (3, 2)
(4, 1)   (1, 9)  (7, 2)

Below is the C++ program to implement the above approach:

C++




// C++ program to demonstrate the
// working of sorting a 2D vector or
// vector of vectors of pairs in
// descending order of number of
// columns in a row
#include <bits/stdc++.h>
using namespace std;
 
// Custom comparator function
bool myComparator(vector<pair<int, int> >& vector1,
                  vector<pair<int, int> >& vector2)
{
    return vector1.size() < vector2.size();
}
 
// Function to print 2D vector elements
void print(vector<vector<pair<int, int> > >
           & myContainer)
{
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer)
    {
        // Each element of the 2D vector is
        // a vector itself
        vector<pair<int, int> > myVector =
                                currentVector;
 
        // Iterating over the vector
        // elements
        cout << "[  ";
        for (auto pr : myVector)
        {
            // Print the element
            cout << "{";
            cout << pr.first << ", " <<
                    pr.second;
            cout << "}  ";
        }
        cout << "]\n";
    }
}
 
// Driver code
int main()
{
    // Declaring a 2D vector of pairs
    // Pairs are of type {char, bool}
    vector<vector<pair<int, int> > > myContainer;
 
    // Initializing vectors of pairs
    vector<pair<int, int> > vect1 =
    {{2, 1}, {4, 3}, {8, 1}};
    vector<pair<int, int> > vect2 =
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}, {16, 18}};
    vector<pair<int, int> > vect3 =
    {{17, 18}, {19, 20}};
    vector<pair<int, int> > vect4 =
    {{25, 26}, {27, 28}, {29, 30}, {31, 32}};
    vector<pair<int, int> > vect5 =
    {{7, 2}};
 
    // Inserting vectors in the 2D vector
    myContainer.push_back(vect1);
    myContainer.push_back(vect2);
    myContainer.push_back(vect3);
    myContainer.push_back(vect4);
    myContainer.push_back(vect5);
 
    // Print the vector elements
    // before sorting
    cout << "Before sorting, ";
    print(myContainer);
 
    // Sorting the 2D vector of pairs
    // in ascending order
    // of number of columns in a row
    sort(myContainer.begin(),
         myContainer.end(), myComparator);
 
    cout << "\n\n After sorting the 2D vector " <<
            "in ascending order of the number of " <<
            "columns in a row, ";
    print(myContainer);
 
    return 0;
}


OutputBefore sorting, myContainer elements: [ {2, 1} {4, 3} {8, 1} ] [ {19, 10} {11, 2} {12, 14} {14, 6} {16, 18} ] [ {17, 18} {19, 20} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ] [ {7, 2} ] After sorting the 2D vector in ascending order of the number of columns in a row, myContainer elements: [ {7, 2} ] [ {17, 18} {19, 20} ] [ {2, 1} {4, 3} {8, 1} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ] [ {19, 10} {11, 2} {12, 14} {14, 6} {16, 18} ]

Case 6: Sorting a 2D Vector of pairs on basis of the number of columns in a row in descending order:

In this type of sorting, a 2D vector is sorted on basis of the number of columns in descending order. This is achieved by passing a third argument, a custom comparator to the “sort()” method.

Example:

Input:
(4, 1)   (1, 9)   (7, 2)
(8, 11)
(1, 6)   (3, 2)                               

Output:
(4, 1)   (1, 9)  (7, 2)
(1, 6)   (3, 2)
(8, 11)  

Below is the C++ program to implement the above approach:

C++




// C++ program to demonstrate the
// working of sorting a 2D vector or
// vector of vectors of pairs in
// descending order of number of
// columns in a row
 
#include <bits/stdc++.h>
using namespace std;
 
// Custom comparator function
bool myComparator(vector<pair<int, int> >& vector1,
                  vector<pair<int, int> >& vector2)
{
    return vector1.size() > vector2.size();
}
 
// Function to print 2D vector elements
void print(vector<vector<pair<int, int> > >
           & myContainer)
{
 
    // Iterating over 2D vector elements
    for (auto currentVector : myContainer)
    {
        // Each element of the 2D vector is
        // a vector itself
        vector<pair<int, int> > myVector =
                                currentVector;
 
        // Iterating over the vector
        // elements
        cout << "[  ";
        for (auto pr : myVector)
        {
            // Print the element
            cout << "{";
            cout << pr.first << ", " <<
                    pr.second;
            cout << "}  ";
        }
        cout << "]\n";
    }
}
 
// Driver code
int main()
{
    // Declaring a 2D vector of pairs
    // Pairs are of type {char, bool}
    vector<vector<pair<int, int> > > myContainer;
 
    // Initializing vectors of pairs
    vector<pair<int, int> > vect1 =
    {{2, 1}, {4, 3}, {8, 1}};
    vector<pair<int, int> > vect2 =
    {{19, 10}, {11, 2}, {12, 14}, {14, 6}, {16, 18}};
    vector<pair<int, int> > vect3 =
    {{17, 18}, {19, 20}};
    vector<pair<int, int> > vect4 =
    {{25, 26}, {27, 28}, {29, 30}, {31, 32}};
    vector<pair<int, int> > vect5 = {{7, 2}};
 
    // Inserting vectors in the 2D vector
    myContainer.push_back(vect1);
    myContainer.push_back(vect2);
    myContainer.push_back(vect3);
    myContainer.push_back(vect4);
    myContainer.push_back(vect5);
 
    // Print the vector elements
    // before sorting
    cout << "Before sorting, ";
    print(myContainer);
 
    // Sorting the first row of 2D
    // vector of pairs in descending
    // order of the first element of pairs
    sort(myContainer.begin(),
         myContainer.end(), myComparator);
 
    cout << "\n\n After sorting the 2D vector " <<
            "in descending order of the number " <<
            "of columns in a row, ";
    print(myContainer);
 
    return 0;
}


OutputBefore sorting, myContainer elements: [ {2, 1} {4, 3} {8, 1} ] [ {19, 10} {11, 2} {12, 14} {14, 6} {16, 18} ] [ {17, 18} {19, 20} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ] [ {7, 2} ] After sorting the 2D vector in descending order of the number of columns in a row, myContainer elements: [ {19, 10} {11, 2} {12, 14} {14, 6} {16, 18} ] [ {25, 26} {27, 28} {29, 30} {31, 32} ] [ {2, 1} {4, 3} {8, 1} ] [ {17, 18} {19, 20} ] [ {7, 2} ]

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!