Sorting 2D Vector in C++ | Set 3 (By number of columns)

20 Dec, 2021
We have discussed some of the cases of sorting 2D vector in below set 1 and set 2.Sorting 2D Vector in C++ | Set 1 (By row and column) Sorting 2D Vector in C++ | Set 2 (In descending order by row and column)More cases are discussed in this articleAs mentioned in one of the article published of this set, A 2D Vector can also have rows with different number of columns. This property is unlike the 2D Array in which all rows have same number of columns. 
CPP
// C++ code to demonstrate 2D Vector// with different no. of columns#include<iostream>#include<vector> // for 2D vectorusing namespace std;int main(){    // Initializing 2D vector "vect" with    // values    vector< vector<int> > vect{{1, 2},                               {3, 4, 5},                               {6}};     // Displaying the 2D vector    for (int i=0; i<vect.size(); i++)    {        //loop till the size of particular        //row        for (int j=0; j<vect[i].size() ;j++)            cout << vect[i][j] << " ";        cout << endl;    }     return 0; }
Output:  
1 2
3 4 5
6
Case 5 : Sorting the 2D Vector on basis of no. of columns in row in ascending order.In this type of sorting, 2D vector is sorted on basis of a no. of column in ascending order. This is achieved by passing a third argument in “sort()” as a call to user defined explicit function. 
CPP
// C++ code to demonstrate sorting of// 2D vector on basis of no. of columns// in ascending order#include<iostream>#include<vector> // for 2D vector#include<algorithm> // for sort()using namespace std; // Driver function to sort the 2D vector// on basis of a no. of columns in// ascending orderbool sizecom(const vector<int>& v1, const vector<int>& v2){    return v1.size() < v2.size();} int main(){    // Initializing 2D vector "vect" with    // values    vector< vector<int> > vect{{1, 2},                               {3, 4, 5},                               {6}};     // Displaying the 2D vector before sorting    cout << "The Matrix before sorting is:\n";    for (int i=0; i<vect.size(); i++)    {        //loop till the size of particular        //row        for (int j=0; j<vect[i].size() ;j++)            cout << vect[i][j] << " ";        cout << endl;    }     //Use of "sort()" for sorting on    //basis of no. of columns in    //ascending order.    sort(vect.begin(), vect.end(), sizecom);     // Displaying the 2D vector after sorting    cout << "The Matrix after sorting is:\n";    for (int i=0; i<vect.size(); i++)    {        //loop till the size of particular        //row        for (int j=0; j<vect[i].size() ;j++)            cout << vect[i][j] << " ";        cout << endl;    }     return 0; }
Output:  
The Matrix before sorting is:
1 2 
3 4 5 
6 
The Matrix after sorting is:
6 
1 2 
3 4 5 
Case 6 : Sorting the 2D Vector on basis of no. of columns in row in descending order.In this type of sorting, 2D vector is sorted on basis of a no. of column in descending order. This is achieved by passing a third argument in “sort()” as a call to user defined explicit function. 
CPP
// C++ code to demonstrate sorting of// 2D vector on basis of no. of columns// in descending order#include<iostream>#include<vector> // for 2D vector#include<algorithm> // for sort()using namespace std; // Driver function to sort the 2D vector// on basis of a no. of columns in// descending orderbool sizecom(const vector<int>& v1, const vector<int>& v2){    return v1.size() > v2.size();} int main(){    // Initializing 2D vector "vect" with    // values    vector< vector<int> > vect{{1, 2},                      {3, 4, 5},                  {6}};     // Displaying the 2D vector before sorting    cout << "The Matrix before sorting is:\n";    for (int i=0; i<vect.size(); i++)    {        //loop till the size of particular        //row        for (int j=0; j<vect[i].size() ;j++)            cout << vect[i][j] << " ";        cout << endl;    }     //Use of "sort()" for sorting on    //basis of no. of columns in    //descending order.    sort(vect.begin(), vect.end(), sizecom);     // Displaying the 2D vector after sorting    cout << "The Matrix after sorting is:\n";    for (int i=0; i<vect.size(); i++)    {        //loop till the size of particular        //row        for (int j=0; j<vect[i].size() ;j++)            cout << vect[i][j] << " ";        cout << endl;    }     return 0; }
Output:  
The Matrix before sorting is:
1 2 
3 4 5 
6 
The Matrix after sorting is:
3 4 5 
1 2 
6 
This article is contributed by Manjeet Singh .If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
surinderdawra388
cpp-vector
STL
C Language
C++
Sorting
Sorting
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Unordered Sets in C++ Standard Template Library
Operators in C / C++
Exception Handling in C++
What is the purpose of a function prototype?
TCP Server-Client implementation in C
Vector in C++ STL
Map in C++ Standard Template Library (STL)
Initialize a vector in C++ (7 different ways)
Set in C++ Standard Template Library (STL)
Priority Queue in C++ Standard Template Library (STL)