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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:38:25+00:00 2026-05-25T14:38:25+00:00

I have a simple C++ code written to understand sort function usage on user

  • 0

I have a simple C++ code written to understand sort function usage on user defined class data types to sort data members of a class.

But this only sorts array of variable b in the class. Because, the earlier sorted array of variable a, is also disturbed whiel sorting b.

#include <iostream>
#include <cstring>
using namespace std;

class Entry
{
public:
int a, b;
};

bool compare1(Entry e1, Entry e2)
{

if (e1. a > e2. a) return false;
return true;

}

bool compare2( Entry e1,  Entry e2)
{

if (e1. b > e2. b) return false;
return true;

}

int main()
{
int i;



vector<Entry> array(4);
array[0]. a =5 , array[0]. b =8 ;
array[1]. a =10 , array[1]. b =4 ;
array[2]. a =3 , array[2]. b =2 ;
array[3]. a =1 , array[3]. b =12 ;



sort(array.begin(), array.end(), compare1);
sort(array.begin(), array.end(), compare2);



cout << "sorted:" << endl;
for (i = 0; i< 4; i++)
cout << array[i]. a << " " << array[i].b << endl;

}

The output I get is as follows:

sorted:
3 2
10 4
5 8
1 12

How to sort both data member arrays – a,b?

  • 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-25T14:38:26+00:00Added an answer on May 25, 2026 at 2:38 pm

    It depends on how you want your elements to be sorted:

    1. Sort as pairs, keyed on a: (1,12), (3,2), (5,8), (10,4)

    2. Sort as pairs, keyed on b: (3,2), (10,4), …

    3. Sort as pairs lexicographically: same as sorting on a, since there are no repeated values for a.

    In case (1) you use compare1, in case (2) you use compare2. (For case (3) you would have to write another predicate, or just use std::pair<int,int>.)

    Case 4: If you want the values of a and b sorted separately and destroy the pairing, then you need to put the values into separate vectors of ints and sort those individually:

    std::vector<int> avals(array.size()), bvals(array.size());
    
    for (size_t i = 0; i != array.size(); ++i)
    {
      avals[i] = array[i].a;
      bvals[i] = array[i].b;
    }
    
    std::sort(avals.begin(), avals.end());
    std::sort(bvals.begin(), bvals.end());
    

    There is no way around this. A container of Entry objects can only move elements around as a whole.

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

Sidebar

Related Questions

am learning C# and have written a simple bit of code, but i don't
I have some code I've written in PHP for consuming our simple webservice, which
I have a simple function that I want to call in the code behind
There is probably is simple fix for this but I currently have code similar
I have written simple code to determine country location using the suffix on the
this code is written in simple ActionScript, but i'm assuming this problem of mine
I have written this simple c code in microsoft visual c++ 2010. #include<stdio.h> #include<conio.h>
I have this code written in jQuery to have a simple on hover slide
I have a very simple client server code written java(server listens on some port
I have written following simple code to see the behavior of rendering. In this

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.