Have a “vector of vectors”
that looks something like this
3 1 2 0 77
0 3 1 2 44
1 0 3 2 29
3 0 1 2 49
I would like to sort them according to the last element in every row so that it would look like this in the end
1 0 3 2 29
0 3 1 2 44
3 0 1 2 49
3 1 2 0 77
Of course my real example is a lot more complex… but this is basically what I need to get done.
Right now I use this snippet which seems to sort according to the first elements.
vector<vector<int>>population;
partial_sort( population.begin(),population.begin()+10, population.end() );
You can use std::sort with a function (or functor object) that provides a strict weak ordering for vectors. I.e. you define a vector-less-than function that orders two vectors correctly, something like this (off the top of my head).
Edit: after comments, added checking for one or two empty vectors, which does make things trickier.