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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:58:25+00:00 2026-05-10T19:58:25+00:00

You can pass a function pointer, function object (or boost lambda) to std::sort to

  • 0

You can pass a function pointer, function object (or boost lambda) to std::sort to define a strict weak ordering of the elements of the container you want sorted.

However, sometimes (enough that I’ve hit this several times), you want to be able to chain ‘primitive’ comparisons.

A trivial example would be if you were sorting a collection of objects that represent contact data. Sometimes you will want to sort by

last name, first name, area code

. Other times

first name, last name

– yet other times

age, first name, area code

… etc

Now, you can certainly write an additional function object for each case, but that violates the DRY principle – especially if each comparison is less trivial.

It seems like you should be able to write a hierarchy of comparison functions – the low level ones do the single, primitive, comparisons (e.g. first name < first name), then higher level ones call the lower level ones in succession (probably chaining with && to make use of short circuit evaluation) to generate the composite functions.

The trouble with this approach is that std::sort takes a binary predicate – the predicate can only return a bool. So if you’re composing them you can’t tell if a ‘false’ indicates equality or greater than. You can make your lower level predicates return an int, with three states – but then you would have to wrap those in higher level predicates before they could be used with std::sort on their own.

In all, these are not insurmountable problems. It just seems harder than it should be – and certainly invites a helper library implementation.

Therefore, does anyone know of any pre-existing library (esp. if it’s a std or boost library) that can help here – of have any other thoughts on the matter?

[Update]

As mentioned in some of the comments – I’ve gone ahead and written my own implementation of a class to manage this. It’s fairly minimal, and probably has some issues with it in general. but on that basis, for anyone interested, the class is here:

http://pastebin.com/f52a85e4f

And some helper functions (to avoid the need to specify template args) is here:

http://pastebin.com/fa03d66e

  • 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. 2026-05-10T19:58:26+00:00Added an answer on May 10, 2026 at 7:58 pm

    You could build a little chaining system like so:

    struct Type {   string first, last;   int age; };  struct CmpFirst {   bool operator () (const Type& lhs, const Type& rhs) { return lhs.first < rhs.first; } };  struct CmpLast {   bool operator () (const Type& lhs, const Type& rhs) { return lhs.last < rhs.last; } };  struct CmpAge {   bool operator () (const Type& lhs, const Type& rhs) { return lhs.age < rhs.age; } };  template <typename First, typename Second> struct Chain {   Chain(const First& f_, const Second& s_): f(f_), s(s_) {}    bool operator () (const Type& lhs, const Type& rhs) {     if(f(lhs, rhs))       return true;     if(f(rhs, lhs))       return false;      return s(lhs, rhs);   }    template <typename Next>   Chain <Chain, Next> chain(const Next& next) const {      return Chain <Chain, Next> (*this, next);   }    First f;   Second s; };  struct False { bool operator() (const Type& lhs, const Type& rhs) { return false; } };  template <typename Op> Chain <False, Op> make_chain(const Op& op) { return Chain <False, Op> (False(), op); } 

    Then to use it:

    vector <Type> v;  // fill this baby up  sort(v.begin(), v.end(), make_chain(CmpLast()).chain(CmpFirst()).chain(CmpAge())); 

    The last line is a little verbose, but I think it’s clear what’s intended.

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

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

In a C++ project that uses smart pointers, such as boost::shared_ptr , what is
I have a object that is my in memory state of the program and
I currently have a method within my class that has to call other methods,
I'm trying to create am immutable type (class) in C++, I made it so

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.