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 835675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:51:42+00:00 2026-05-15T04:51:42+00:00

I currently have a std::vector which holds std::vector of double. I’d want to sort

  • 0

I currently have a std::vector which holds std::vector of double. I’d want to sort it by the second element of the double vectore. ex:
instead of sorting by MyVec[0] or myvec[1] I wat it to sort myVec[0] and myvec[1] based on myvec[0][1] myvec[1][1]. Basically sort by a contained value, not the objects in it.

so if myvec[0][1] is less than myvec[1][1] then myvec[0] and myvec[1] will swap.
Thanks

  • 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-15T04:51:43+00:00Added an answer on May 15, 2026 at 4:51 am

    Just write the comparator:

    bool my_comparator(const std::vector<double>& lhs, const std::vector<double>& rhs)
    {
      assert(lhs.size() >= 2 && rhs.size() >= 2);
      return lhs[1] < rhs[1];
    }
    ...
    std::sort(big_vector.begin(), big_vector.end(), my_comparator);
    

    It is better to write a functor instead of a function, but the concept is the same.

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

Sidebar

Related Questions

I have a comma separated integers and I want to store them in std::vector<int>
I have an input file that I want to sort based on timestamp which
Background I have a container class which uses vector<std::string> internally. I have provided a
I have a std::list which I am currently randomizing using a Fisher-Yates shuffle (see
In my current setup, I have a typedef std::function<void (MyClass&, std::vector<std::string>) MyFunction; std::map<std::string, MyFunction>
Currently, I have some code as follows template<typename Type> Type* getValue(std::string name, bool tryUseGetter
I currently have a table of concentrations, which are linked to a table of
I currently have a deployed app (fortworth.herokuapp.com) that I am attempting to sort movies
I have defined a class in C++ which holds an array of scalars of
A different question inspired the following thought: Does std::vector<T> have to move all the

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.