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

The Archive Base Latest Questions

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

I’m working on a project for school and need to sort some data. I’ve

  • 0

I’m working on a project for school and need to sort some data. I’ve been given a vector of objects and I have to sort the objects (either in place or using an index) based on one of their properties. There are several different objects and several different properties that could it be sorted by. What’s the best way to go about doing this?

  • 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-20T11:11:06+00:00Added an answer on May 20, 2026 at 11:11 am

    There are several different objects and several different properties that could it be sorted by.

    While the solution Erik posted is correct, this statement leads me to think that it’s impractical at best if you are in fact planning to sort by multiple public data members of multiple classes in multiple ways in the same program, as each sorting method will require its own functor type.

    I recommend the following abstraction:

    #include <functional>
    
    template<typename C, typename M, template<typename> class Pred = std::less>
    struct member_comparer : std::binary_function<C, C, bool> {
        explicit member_comparer(M C::*ptr) : ptr_{ptr} { }
    
        bool operator ()(C const& lhs, C const& rhs) const {
            return Pred<M>{}(lhs.*ptr_, rhs.*ptr_);
        }
    
    private:
        M C::*ptr_;
    };
    
    template<template<typename> class Pred = std::less, typename C, typename M>
    member_comparer<C, M, Pred> make_member_comparer(M C::*ptr) {
        return member_comparer<C, M, Pred>{ptr};
    }
    

    Usage would look like:

    #include <algorithm>
    #include <string>
    #include <vector>
    
    struct MyClass {
        int         i;
        std::string s;
    
        MyClass(int i_, std::string const& s_) : i{i_}, s{s_} { }
    };
    
    int main() {
        std::vector<MyClass> vec;
        vec.emplace_back(2, "two");
        vec.emplace_back(8, "eight");
    
        // sort by i, ascending
        std::sort(vec.begin(), vec.end(), make_member_comparer(&MyClass::i));
        // sort by s, ascending
        std::sort(vec.begin(), vec.end(), make_member_comparer(&MyClass::s));
        // sort by s, descending
        std::sort(vec.begin(), vec.end(), make_member_comparer<std::greater>(&MyClass::s));
    }
    

    This will work for any type with public data members, and will save a lot of typing if you need to sort your classes more than a couple of different ways.

    Here is a variation that works with public member functions instead of public data members:

    #include <functional>
    
    template<typename C, typename M, template<typename> class Pred = std::less>
    struct method_comparer : std::binary_function<C, C, bool> {
        explicit method_comparer(M (C::*ptr)() const) : ptr_{ptr} { }
    
        bool operator ()(C const& lhs, C const& rhs) const {
            return Pred<M>{}((lhs.*ptr_)(), (rhs.*ptr_)());
        }
    
    private:
        M (C::*ptr_)() const;
    };
    
    template<template<typename> class Pred = std::less, typename C, typename M>
    method_comparer<C, M, Pred> make_method_comparer(M (C::*ptr)() const) {
        return method_comparer<C, M, Pred>{ptr};
    }
    

    With usage like:

    #include <algorithm>
    #include <string>
    #include <vector>
    
    class MyClass {
        int         i_;
        std::string s_;
    
    public:
        MyClass(int i, std::string const& s) : i_{i}, s_{s} { }
    
        int                i() const { return i_; }
        std::string const& s() const { return s_; }
    };
    
    int main() {
        std::vector<MyClass> vec;
        vec.emplace_back(2, "two");
        vec.emplace_back(8, "eight");
    
        // sort by i(), ascending
        std::sort(vec.begin(), vec.end(), make_method_comparer(&MyClass::i));
        // sort by s(), ascending
        std::sort(vec.begin(), vec.end(), make_method_comparer(&MyClass::s));
        // sort by s(), descending
        std::sort(vec.begin(), vec.end(), make_method_comparer<std::greater>(&MyClass::s));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.