C++ sort

Jimmy (xiaoke) Shen
2 min readFeb 14, 2020

--

https://www.geeksforgeeks.org/sorting-2d-vector-in-c-set-1-by-row-and-column/

// C++ code to demonstrate sorting of a 
// 2D vector on basis of a column
#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 particular column
bool sortcol( const vector<int>& v1,
const vector<int>& v2 ) {
return v1[1] < v2[1];
}
int main()
{
// Initializing 2D vector "vect" with
// values
vector< vector<int> > vect{{8, 5, 1},
{4, 8, 6},
{7, 2, 9}};
// Number of rows;
int m = vect.size();
// Number of columns (Assuming all rows
// are of same size). We can have different
// sizes though (like Java).
int n = vect[0].size();

// Displaying the 2D vector before sorting
cout << "The Matrix before sorting is:\n";
for (int i=0; i<m; i++)
{
for (int j=0; j<n ;j++)
cout << vect[i][j] << " ";
cout << endl;
}
// Use of "sort()" for sorting on basis
// of 2nd column
sort(vect.begin(), vect.end(),sortcol);
// Displaying the 2D vector after sorting
cout << "The Matrix after sorting is:\n";
for (int i=0; i<m; i++)
{
for (int j=0; j<n ;j++)
cout << vect[i][j] << " ";
cout << endl;
}
return 0;
}

Output

The Matrix before sorting is:
8 5 1
4 8 6
7 2 9
The Matrix after sorting is:
7 2 9
8 5 1
4 8 6

In the default mode, if we don’t customize the compare function, the first column will be sorted.

The Matrix before sorting is:
8 5 1
4 8 6
7 2 9
The Matrix after sorting is:
4 8 6
7 2 9
8 5 1

We can also write a lambda expression

// C++ code to demonstrate sorting of a 
// 2D vector on basis of a column
#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 particular column
bool sortcol( const vector<int>& v1,
const vector<int>& v2 ) {
return v1[1] < v2[1];
}
int main()
{
// Initializing 2D vector "vect" with
// values
vector< vector<int> > vect{{8, 5, 1},
{4, 8, 6},
{7, 2, 9}};
// Number of rows;
int m = vect.size();
// Number of columns (Assuming all rows
// are of same size). We can have different
// sizes though (like Java).
int n = vect[0].size();

// Displaying the 2D vector before sorting
cout << "The Matrix before sorting is:\n";
for (int i=0; i<m; i++)
{
for (int j=0; j<n ;j++)
cout << vect[i][j] << " ";
cout << endl;
}
// Use of "sort()" for sorting on basis
// of 2nd column
sort(vect.begin(), vect.end(),[](const vector<int>&v1, const vector<int>&v2){
return v1[1]<v2[1];
});
// Displaying the 2D vector after sorting
cout << "The Matrix after sorting is:\n";
for (int i=0; i<m; i++)
{
for (int j=0; j<n ;j++)
cout << vect[i][j] << " ";
cout << endl;
}
return 0;
}

--

--

No responses yet