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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:00:11+00:00 2026-05-31T11:00:11+00:00

I have a vector that that stores Node*. A Node has the member properties:row,col,value.

  • 0

I have a vector that that stores Node*. A Node has the member properties:row,col,value. In my vector, I have gotten it to where all the Nodes* within the same row but not necessarily in correct column order. So basically I want to sort it so it’s truly in row-major form. Right now, the columns are out of order within each “row”. I appreciate any help you could give!

Edit:

Here is the method I have that is sorting my vector by row. Is there a way to additionally sort the columns as well?

    vector<Node*> vect;
    int i,j,minIndex;
    Node* temp = new Node(NULL,NULL,0,0,0);
    for(i=0;i<vect.size()-1;i++)
    {
        minIndex = i;
        for(j=i+1;j<vect.size();j++)
        {
            if(vect.at(j)->row<vect.at(minIndex)->row)
            {
                minIndex = j;
            }
        }
        if(minIndex!=i)
        {
            temp = vect.at(i);
            vect.at(i) = vect.at(minIndex);
            vect.at(minIndex) = temp;
        }
    }
  • 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-31T11:00:13+00:00Added an answer on May 31, 2026 at 11:00 am

    You don’t really need to implement your own sorting algorithm here. You can just use the standard template library’s sort() method and then override default the behavior for sorting your Node* vector.

    // This returns true if Node* a should be considered "less than" Node* b.
        struct less_node : binary_function <Node*,Node*,bool> 
        {
            bool operator() (const Node*& a, const Node*& b) const
            {
                    // sort by row first
                if (a->row < b->row)  
                    return true;
                    // then sort by col within each row
                if (a->row == b->row && a->col < b->col)
                    return true;
                return false;
            }
        };
    

    Once you have this less_node structure defined, you would just call:

    sort(vect.begin(), vect.end(), less_node());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector t_vec that stores references to instances of class Too .
I have a Vector of Object[] type that stores my data. How to print
I have a vector that stores pointers to many objects instantiated dynamically, and I'm
I have a system that stores vectors and allows a user to find the
I have a vector that I want to insert into a set . This
I have a Vector that holds a number of objects. My code uses a
I have a vector X that contains positive numbers that I want to bin/discretize.
I have a vector-like class that contains an array of objects of type T
I have a vector of beans that holds information I want to display in
I have a sparsely populated vector that I populated via hashing, so elements are

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.