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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:05:15+00:00 2026-06-02T21:05:15+00:00

I have a class with a function MyClass::doStuff(std::vector<MyCustomData*> toSort) { … in which I

  • 0

I have a class with a function

MyClass::doStuff(std::vector<MyCustomData*> toSort) { ...

in which I call

std::sort(toSort.begin(), toSort.end(), MyClass::SortByZ());

myClass::SortByZ() is a custom comparator.
Now this works but I would like to achieve the following:

I have several classes, which should each have its own comparator functor to sort “MyCustomData”. So e.g. Class1… should have

class Class1 {
    struct SortData {
        bool operator ()(MyCustomData *lhs, MyCustomData *rhs) {
        return lhs->something1 > rhs->something1;
        }
    };
    //...many more functions/vars
}

while Class2 has a different comparator functor for the same datatype eg

class Class2 {
    struct SortData {
        bool operator ()(MyCustomData *lhs, MyCustomData *rhs) {
        return lhs->something2 > rhs->something2;
        }
    };
    //...many more functions/vars
}

Now I would like to be able to call the function MyClass::doStuff(…) with either

doStuff(myData, Class1::SortData)

or

doStuff(myData, Class2::SortData)

and the function MyClass::doStuff(…) should use the respective Sort-Order.

I did not find out a way of doing this, is there one? I would like a simple solution (doesn’t have to support templates or anything). I would be willing to use boost if I needed that, but a solution without boost would be preferred.

I hope I was able to describe what I want to achieve? Thanks for any help!

  • 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-02T21:05:17+00:00Added an answer on June 2, 2026 at 9:05 pm

    You will have to make doStuff a template:

    template <typename Comparator>
    void doStuff(std::vector<MyCustomData*> toSort, Comparator compare) {
       // ...
       std::sort(toSort.begin(), toSort.end(), compare);
       // ...
    }
    

    Also, it might want to take the first argument by reference. As it is, it will sort a copy of the argument, discard that copy, and leave the caller’s vector untouched; although perhaps that’s what you want.

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

Sidebar

Related Questions

I have an inline member function defined under class MyClass int MyClass::myInlineFunction(); This function
Suppose I have a class like this: function myClass(q) { this.someFunction = function(e) {
I have a function that calls two other functions: class myClass{ function myFunc(){ for($i=0;$i<500;$i++){
I have A.php and B.php A.php <?php error_reporting(E_ALL); ini_set(display_errors,1); class myClass { function hello()
Right now I have a normal class like this class MyClass { function myfunc1
i have a class like this: class myclass { public function save($params){ // some
I have a custom class that looks like: function myClass(){ var thing; var thingtype;
I have this PHP class class myclass{ function name($val){ echo this is name method
I have a class like this : var MyClass = function(){ this.field = 'field';
I have this code: var MyClass = new Class({ Implements: [Options,Events], initialize: function(){ },

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.