I am having trouble to figure out, how to sort a vector of vector of strings, here is the testing code.
#include <iostream>
#include <vector>
#include <boost/algorithm/string.hpp>
int main(int argc, char** argv) {
std::vector <std::vector <std::string> > data_var;
std::vector <std::string> temp;
std::string str1 = "1,hello3,temp2";
std::string str2 = "2,hello2,temp1";
std::string str3 = "3,hello1,temp3";
boost::split(temp, str1, boost::is_any_of(","));
data_var.push_back(temp);
boost::split(temp, str2, boost::is_any_of(","));
data_var.push_back(temp);
boost::split(temp, str3, boost::is_any_of(","));
data_var.push_back(temp);
// sorting code here...
}
Thanks in advance…
If you only want to sort based on the second column, then you just need to provide a custom comparison operator. Once way to do that is:
Edit: If you won’t know until runtime which column you’ll be sorting on, you can encode that in the sorting object:
Notice how we’ve added a constructor that takes which column it’ll act on. You can use it like this:
You don’t even need to make the local variable if you don’t want to:
[Code at ideone]