# How to sort a 2D vector in C++

In this tutorial, we are going to learn how to sort a 2D vector in C++. Here we will learn about the 2D vector, how to sort a specific row in 2D vector, and how to sort the entire 2D vector on the basis of a particular column. We will also see the C++ program for the same.

**Here all the sorting are done in ascending order**

## 2D Vector

Vectors are known as dynamic arrays, they have the ability to resize itself automatically when an element is inserted or deleted.

A 2D vector is the vector of vectors with a variable number of rows where each row is a vector. Every index of vector contains a vector that can be traversed and accessed by using iterators.

For example:-

[[1,2,3],

[4,5,6],

[7,8,9],

[10,11,12]]

## How to sort a specific row in a 2D Vector

It is very easy to sort a specific row of a 2D vector by using a sort() function. We have to pass the iterator of that specific row (1D vector) to sort() function and this will sort that particular row of the 2D vector.

Let us see the code:-

#include<bits/stdc++.h> using namespace std; /*============================================ FUNCTION TO SORT A PARTICULAR ROW OF 2D VECTOR =============================================*/ void sorting(vector<vector<int>>A) { //n contain no.of rows int n=A.size(); //m contain no. of columns int m=A[0].size(); // Displaying the 2D vector after sorting cout << "2D vector before sorting 2nd row :\n"; for (int i=0; i<n; i++) { for (int j=0; j<m ;j++) { cout << A[i][j] << " "; }cout<<endl; } // Using sort() function to sort. //A[1] is 1D vector of 2nd row. sort(A[1].begin(), A[1].end()); // Displaying the 2D vector after sorting cout << "2D vector after sorting 2nd row :\n"; for (int i=0; i<n; i++) { for (int j=0; j<m ;j++) { cout << A[i][j] << " "; }cout << endl; } } /*====================================== MAIN FUNCTION =======================================*/ int main() { // Initializing 2D vector vector< vector<int> > vect{{6, 4, 1}, {9, 8, 2}, {1, 2, 9}}; //Calling sorting function sorting(vect); return 0; }

**Output:-**

2D vector before sorting 2nd row : 6 4 1 9 8 2 1 2 9 2D vector after sorting 2nd row : 6 4 1 2 8 9 1 2 9

## How to sort the entire 2D Vector on the basis of a particular column?

To sort a 2D vector on the basis of a particular column means that the entire row which contains the smallest element of that particular column will become the first row. In similar way, the row that contains the largest element of that particular column will become the last row.

Example:-

6 4 1 1 2 9

9 8 2 ====> 6 4 1

1 2 9 9 8 2

To sort the 2D vector in this way we add one more parameter to sort function which is used for comparison. It calls a function that compares the elements of that particular column and sort the 2D vector accordingly.

Let us see the code:-

#include<bits/stdc++.h> using namespace std; /*============================================ COMPARE FUNCTION COMAPRE 2ND COLUMN ELEMENTS =============================================*/ bool compare_col(const vector<int>& v1,const vector<int>& v2) { return v1[1] < v2[1]; } /*========================================================== FUNCTION TO SORT 2D VECTOR ON THE BASIS OF PARTICULAR COLUMN ===========================================================*/ void sorting(vector<vector<int>>A) { //n contain no.of rows int n=A.size(); //m contain no. of columns int m=A[0].size(); cout<<"Sorting 2D vector on the basis of 2nd column\n"; // Displaying the 2D vector after sorting cout << "2D vector before sorting :"<<endl; for (int i=0; i<n; i++) { for (int j=0; j<m ;j++) { cout << A[i][j] << " "; }cout<<endl; } // Using sort() function to sort. //compare_col parameter used to compare col elements. //And sort accordingly. sort(A.begin(), A.end(),compare_col); // Displaying the 2D vector after sorting cout << "2D vector after sorting :"<<endl; for (int i=0; i<n; i++) { for (int j=0; j<m ;j++) { cout << A[i][j] << " "; }cout << endl; } } /*====================================== MAIN FUNCTION =======================================*/ int main() { // Initializing 2D vector vector< vector<int> > vect{{6, 4, 1}, {9, 8, 2}, {1, 2, 9}}; //Calling sorting function sorting(vect); return 0; }

**Output:-**

Sorting 2D vector on the basis of 2nd column 2D vector before sorting : 6 4 1 9 8 2 1 2 9 2D vector after sorting : 1 2 9 6 4 1 9 8 2

Thanks for reading this tutorial. I hope it helps you !!

How can make user choose which column to sort with?