How to sort a mutlidimensional vector

Hello, I’m trying to sort a 2D vector, only considering the first value of the vector.
< vector< vector < int > > sortingVector;
Do you have any idea how to do this? Especially without using c++11? (the only solution google showed me)
Thank you!

Hi there,

Using the comparison function.

For example:

struct compare_2d_vector{
	bool operator()(const vector<int>& v1, const vector<int>& v2){
		if(v1.begin() != v1.end() && v2.begin() != v2.end()){
			return (v1[0] < v2[0]);
		}else{
			return false;
		}
	}
};

Then you can used STL::sort to sort your 2D vector.

sort(sortingVector.begin(), sortingVector.end(), compare_2d_vector());
1 Like

okay thank you very much!