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

  • Home
  • SEARCH
  • 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 9030387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:28:42+00:00 2026-06-16T07:28:42+00:00

I have a 2-d array, containing only 0 or 1. I want to sort

  • 0

I have a 2-d array, containing only 0 or 1.
I want to sort it in descend order on rows (no change on each column) using STL sort algorithm. but I don’t know how to pass the parameter and how to write the compare function in sort(first, last, comp);
like:

0 1 1 1
1 1 0 1
1 0 1 0

would be sorted like this:

1 1 0 1
1 0 1 0
0 1 1 1

my data structure is like this:

int **table = 0;
table = new int *[row];
for(int i=0;i<row;i++)
table[i] = new int[column];

I can only write the sort function like this:

sort(a[0], a[0]+row, compare_function);

bool compare_function(int a[], int b[])
{
    int i =0;
    while(a[i]==0 ||a[i]==1)
    {
        if(a[i]>b[i])
            return true;
        else
            i++;
    }
    return false;
}

But it doesn’t work. Can some one help me?
Thank you very much.

  • 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-06-16T07:28:45+00:00Added an answer on June 16, 2026 at 7:28 am

    Your call to sort looks wrong to me (though you never said what a is). It should be sort(table, table+row, compare_function)

    But I’d do it a bit differently anyway (std::lexicographical_compare comes from <algorithm>):

    struct compare_rows {
      const int cols;
      compare_rows(int cols_) : cols(cols_) {}
      bool operator()(const int a[], const int b[]) const {
        // a b reversed to give descending sort
        return lexicographical_compare(b, b+cols, a, a+cols);
        }
      };
    

    And use it like:

    sort(table, table+row, compare_rows(column))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have both matrices containing only ones and each array has 500 rows and
I have an array of variables all containing text. I want to select only
I have an array containing a list of files. I want to sort it
I have an array containing only booleans: $l = array(0=>true,2=>false,3=>true,4=>true,5=>true); I want to achieve
I have an array containing specific times and I want when the current time
Ruby 1.8.6 I have an array containing numerical values. I want to reduce it
I have a date array sorted in asc order. I want to display the
I have an array containing URL paths, I want to be able to search
I have a javascript array containing the name and values of radios I want
I've got an array containing lots of Color and I want to change 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.