Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6325801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:57:45+00:00 2026-05-24T16:57:45+00:00

I am having trouble to figure out, how to sort a vector of vector

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:57:46+00:00Added an answer on May 24, 2026 at 4:57 pm

    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:

    struct StringListCompare
    {
      bool operator()(const vector<string>& lhs, const vector<string>& rhs)
      {
        // what do we do if lhs or rhs don't have two elements?
        if (lhs.size() < 2 || rhs.size() < 2)
        {
          // ?
        }
        else
        {
          return lhs[1] < rhs[1];
        }
      }
    } StringListComparer;
    
    int main()
    {
      // ...
      sort(data_var.begin(), data_var.end(), StringListComparer);
    }
    

    Edit: If you won’t know until runtime which column you’ll be sorting on, you can encode that in the sorting object:

    class StringListCompare
    {
    public:
      explicit StringListCompare(int column) : m_column(column) {}
      bool operator()(const vector<string>& lhs, const vector<string>& rhs)
      {
        // what do we do if lhs or rhs don't have (m_column + 1) elements?
        return lhs[m_column] < rhs[m_column];
      }
    private:
      int m_column;
    };
    

    Notice how we’ve added a constructor that takes which column it’ll act on. You can use it like this:

      // We set it up so the columns are 0-based:
      StringListCompare compare_column_0(0), compare_column_1(1), compare_column_2(2);
    
      cout << "Original:\n" << data_var << endl;
      sort(data_var.begin(), data_var.end(), compare_column_2);
      cout << "Sorted on column 2:\n" << data_var << endl;
      sort(data_var.begin(), data_var.end(), compare_column_1);
      cout << "Sorted on column 1:\n" << data_var << endl;
      sort(data_var.begin(), data_var.end(), compare_column_0);
      cout << "Sorted on column 0:\n" << data_var << endl;
    

    You don’t even need to make the local variable if you don’t want to:

      sort(data_var.begin(), data_var.end(), StringListCompare(2));
      cout << "Sorted on 2, no local sort variable:\n" << data_var << endl;
    

    [Code at ideone]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have having trouble figure out how to write this code. I have a
Im having trouble trying to figure out some code someone has suggested and was
I am really having trouble trying to figure out how to find certain records
I am having some trouble trying to figure out, how to accomplish the following.
I am having some trouble trying to figure out how to parse information collected
I'm having trouble with the MyClass::function(); style of calling methods and can't figure out
I'm having a bit of a trouble trying to figure this out today, i
I'm having trouble trying to figure out how to get prolog to spit out
I am having trouble finding out how to figure out if an HTML dropdown
i'm having trouble working out the returned values of the below code pMeasure =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.