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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:41:30+00:00 2026-05-22T22:41:30+00:00

I have a problem sorting vectors in 2D vector? I would like to sort

  • 0

I have a problem sorting vectors in 2D vector? I would like to sort them based on their capacity from the largest to the smallest.

Example: ROWS {{1,2,3},{1,2},{1,2,3,4,5}}

it should be sorted as ROWS.capacity(); // ROWS {{1,2,3,4,5},{1,2,3},{1,2}}

The following is part of the code i did until now:

 std::vector< std::vector<int> >::iterator row;
 std::vector<int>::iterator col;



for (row=ROWS.begin(); row<ROWS.end(); row++){

Logger::logln(LOG_DEBUG, "ROW: %d",row->capacity());

     for (col = row->begin(); col != row->end(); col++){
         Logger::logln(LOG_DEBUG, " CONTENT: %d  ",*col);
     }
 }

i need to the following:
if(row1.capacity > row2.capacity)
then swap or something like this.

Thanks in advance 🙂

  • 1 1 Answer
  • 1 View
  • 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-22T22:41:30+00:00Added an answer on May 22, 2026 at 10:41 pm

    You could use std::sort with a custom ordering predicate:

    struct CapacityGreater : public std::binary_function<std::vector<int>,
                                                         std::vector<int>,bool>
    {
        bool operator()(const std::vector<int> &a, const std::vector<int> &b) const
            { return a.capacity() > b.capacity(); }
    };
    
    std::sort(ROWS.begin(), ROWS.end(), CapacityGreater());
    

    This should work well if std::sort uses std::swap internally, otherwise the copying of rows could get quite expensive and you probably need to implement your own sorting function.

    You should also think, if you really need capacity() and not size().

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

Sidebar

Related Questions

I have a problem with how the List Sort method deals with sorting. Given
I am having a problem sorting results from joining tables that have to be
I have a problem with sorting NSTableColumn contents. In my NSTableView there are three
I have a problem with sorting items in table in PHP. Here is what
I have a complex sorting problem with my SQL statement. I have a table
I have a problem on using LinqToSQL with paging + dynamic sorting. These are
I am currently faced with a difficult sorting problem. I have a collection of
I have a problem getting sorting to work with multiple joined tables. For example:
I have a problem with Gridview sorting that is similar to others but I'm
Problem I have timestamped data, which I need to search based on the timestamp

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.