Believe it or not, when I search this, I come up with nada.
How can I sort a multidimensional vector of ints by one of the “columns”?
Many thanks in advance!
C++
res = mysql_perform_query(conn, "SELECT column1, column2, column3 FROM table1;");
std::vector< std::vector< int > > myVector;
while ((row = mysql_fetch_row(res)) !=NULL){
int rankedID = atoi(row[0]);
std::vector< int > tempRow;
tempRow.push_back(atoi(row[0]));
tempRow.push_back(atoi(row[1]));
tempRow.push_back(atoi(row[2]));
myVector.push_back(tempRow);
}
I’d like to sort myVector by myVector[i][1] descending.
Thanks again!
Please notice that you will need a C++11 compiler to get this code compile. You should make the lambda function accept const references to avoid expensive copies, as suggested by Blastfurnace.
Output of the program: