Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sorting vector of vector of strings in C++

I am having trouble to figure out, how to sort a vector of vector of strings, here is the testing code.


#include &ltiostream>
#include &ltvector>
#include &ltboost/algorithm/string.hpp>

int main(int argc, char** argv) {
  std::vector &ltstd::vector &ltstd::string> > data_var;
  std::vector &ltstd::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...

like image 745
rda3mon Avatar asked Aug 18 '11 21:08

rda3mon


People also ask

How do you sort vector vector strings?

Approach: The sort() function in C++ STL is able to sort vector of strings if and only if it contains single numeric character for example, { '1', ' '} but to sort numeric vector of string with multiple character for example, {'12', '56', '14' } one should write own comparator inside sort() function.

Can we sort string vector in C++?

A vector in C++ can be easily sorted in ascending order using the sort() function defined in the algorithm header file. The sort() function sorts a given data structure and does not return anything. The sorting takes place between the two passed iterators or positions.

How do you sort a vector of string in descending order?

Sorting a vector in descending order can be done by using std::greater <>().


1 Answers

Alright: new -simpler- answer, having learned that vectors are comparable:

//sorting code here...
std::sort(data_var.begin(), data_var.end(), std::greater<std::vector<std::string>>());
like image 136
Mooing Duck Avatar answered Nov 12 '22 10:11

Mooing Duck