C++ Sort vector of structs

In this post, we will discuss how we can sort a vector of structure ( or any object ).

The best way to sort a vector of structure is to use std:sort(). It is part of algorithm.h library. Suppose you have a structure as follows

struct Student{
	int roll;
	string name;
};

You need to sort the vector of Student in ascending order of their roll numbers.

sort(v.begin(), v.end(), [](Student a, Student b){
	return a.roll < b.roll;	
});

The above line of code sort the vector of Student v in ascending order of their roll number. The first two-parameter defines the range and the third parameter is a lambda expression.

The std::sort() sorts all the elements in the range [ v.begin(), v.end() ). The std::sort() compares the elements of the vector using the Lambda Expression. If the Lambda function returns true, then “Student a” appears before “Student b” otherwise “Student b” appears before “Student a” in the sorted list.

Example,

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Student {
	int roll;
	string name;
};

void Print(vector<Student>& v) {

	for (int i = 0; i < v.size(); ++i) {
		cout << v[i].roll << ' ' << v[i].name << endl;
	}
	cout << endl;

}

int main() {

	vector<Student> v;
	v.push_back({ 27, "Ravil" });
	v.push_back({ 46, "Ryland" });
	v.push_back({ 2, "Shane" });
	v.push_back({ 5, "James" });
	v.push_back({ 34, "Tati" });
	v.push_back({ 9, "Jeffree" });

	cout << "Vector Before Sort:\n" << endl;
	Print(v);

	sort(v.begin(), v.end(), [](Student a, Student b) {
		return a.roll < b.roll;
	});

	cout << "Vector After Sort:\n" << endl;
	Print(v);

}

Output

Vector Before Sort:

27 Ravil
46 Ryland
2 Shane
5 James
34 Tati
9 Jeffree

Vector After Sort:

2 Shane
5 James
9 Jeffree
27 Ravil
34 Tati
46 Ryland

Note: To sort vector in descending order, just change the ‘<‘ to ‘>’ in the lambda expression.

Instead of defining the Lambda Expression inside the std::sort() function, we can define lambda expression as a separate function.

bool Compare(Student a, Student b){
	return a.roll > b.roll;
}

After defining the lambda function, we pass it as a parameter to std::sort().

sort(v.begin(), v.end(), Compare);

Example,

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Student{
	int roll;
	string name;
};

bool Compare(Student a, Student b){
	return a.roll > b.roll;
}

void Print(vector<Student> &v){
	
	for(int i=0;i<v.size();++i){
		cout<<v[i].roll<<' '<<v[i].name<<endl;
	}
	cout<<endl;
	
}

int main() {

	vector<Student> v;
	v.push_back({27, "Ravil"});
	v.push_back({46, "Ryland"});
	v.push_back({2, "Shane"});
	v.push_back({5, "James"});
	v.push_back({34, "Tati"});
	v.push_back({9, "Jeffree"});
	
	cout<<"Vector Before Sort:\n"<<endl;
	Print(v);

	sort(v.begin(), v.end(), Compare);
	
	cout<<"Vector After Sort:\n"<<endl;
	Print(v);

}

Output

Vector Before Sort:

27 Ravil
46 Ryland
2 Shane
5 James
34 Tati
9 Jeffree

Vector After Sort:

46 Ryland
34 Tati
27 Ravil
9 Jeffree
5 James
2 Shane

You can also use this method to sort vector of class. If you face any issue, let us know in the comments.

2 thoughts on “C++ Sort vector of structs”

  1. I cannot get the following to compile:

    sort(v.begin(), v.end(), [](Student a, Student b) {
    return a.roll < b.roll;
    });

    "[" gives me an error

    1. It’s working on my PC. I think it’s because of C++ version. I think older versions on C++ do not support lambda expressions.
      Try using C++14. It should work.

Leave a Comment

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