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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:15:30+00:00 2026-06-18T08:15:30+00:00

I am trying to obtain a sorting of class objects according to two of

  • 0

I am trying to obtain a sorting of class objects according to two of its members in such a way: sort according to a member. If two objects have the same value (or if the difference is below some threshold) then search according to the member b. I am using multiset with that.

Problem is, that when another object comes, is checked according to sorting class, and fulfills to return true (to be put higher than the object that is compared to), it might also fulfill with another one being one step higher. How can I introduce here, that it should check with as many previous objects, and go as high as is only possible? (to obtain really well sorted multiset!)
Below how I wrote the sorting class. Please provide me with some nice idea!

And I had problems to introduce indentations for this part of code, anyone can help me to edit please? it’s unreadable.

std::multiset<classA , classA ::SortClass> mydata;

    class SortClass
        {
        public:
            bool operator() (const classA &pt1, const classA &pt2)
                {
                if(pt1.a < pt2.a)
                    {
                    if(abs(pt1.a-pt2.a) < 0.01)
                        {
                        if(pt1.b > pt2.b)
                            return true;
                        else 
                            return false;
                        }
                    else 
                        return true;
                    }
                else
                    return false;
                }
        };

EDIT:

Paste this and see what I want and cannot achieve. I want it sorted increasing with m_a and decreasing with m_b if m_a are the same (you can put there equality or inequality, does not work either)

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


class classA
    {
    protected:
        double m_b;
        double m_a;

    public:
        double get_b() {return m_b;}
        double get_a() {return m_a;}
        void set_b(double b) {m_b = b;}
        void set_a(double a) {m_a = a;}    

        class SortClass
            {
            public:
                bool operator() (const classA &pt1, const classA &pt2)
                    {
                    if(pt1.m_a < pt2.m_a)
                        {
                        if(pt2.m_a - pt1.m_a == 0)
                            return (pt1.m_b > pt2.m_b);
                        else
                            return (pt1.m_a < pt2.m_a);
                        }
                    else
                        return false;
                    }
            };
    };
int main()
    {

    std::multiset<classA, classA::SortClass> mydata;
    classA objA;
    for(int i=0; i<100;i++)
        {
        objA.set_a(rand() %100);
        objA.set_b(rand() %10);
        mydata.insert(objA);
        }

    return 0;
    }
  • 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-18T08:15:31+00:00Added an answer on June 18, 2026 at 8:15 am

    Let’s stop using true, and false and return the bool expressions. It make your code so much easier to read.

        bool sortClassA (const classA &pt1, const classA &pt2)
        {
            if(pt2.m_a == pt1.m_a) {
                return pt1.m_b > pt2.m_b;
            }
            return pt1.m_a > pt2.m_a;
        };
    

    Also, you created this issue you have with the threshold. Simple lower the threshold to something close to zero and the issue will go away, else you’ll have to live with it. I don’t see any other way.

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

Sidebar

Related Questions

I am trying to obtain class information on a field inside a model, when
OK, I have been trying to obtain the id of a particular link, but
I have been trying to obtain the image brightness in Opencv, and so far
I am trying to obtain Spring-defined Hibernate Configuration and SessionFactory objects in my non-Spring
I've been trying to obtain the Driver class for JDBC connection to MySQL. The
I'm trying to obtain a list of all Machines that are members of a
I'm trying to obtain the n-th elements from a list of tuples. I have
I am trying to obtain counts of each combination of levels of two variables,
I'm trying to obtain the values between two relational operation from an image. A(34,67,4)
When trying to obtain an array of objects from an IEnumerable collection of objects

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.