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

  • Home
  • SEARCH
  • 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 9005431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:08:35+00:00 2026-06-16T01:08:35+00:00

im trying to use a sort function to sort a list i have containing

  • 0

im trying to use a sort function to sort a list i have containing pairs comparing their second value. this is what i am using:

std::sort(score_list.begin(), score_list.end(), compare_pair);

This is the sort function:

bool Highscore::compare_pair (std::pair<std::string, int> first, std::pair<std::string, int> second)

{
  if (first.second<second.second) return true;
  else return false;
}

and i am getting this error message:

error: no matching function for call to ‘sort(std::list<std::pair<std::basic_string<char>, int> >::iterator, std::list<std::pair<std::basic_string<char>, int> >::iterator, <unresolved overloaded function type>)’

Any advice? Thanks

  • 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-16T01:08:36+00:00Added an answer on June 16, 2026 at 1:08 am

    You can’t pass a member function directly as the comparator. When you use a function, what’s actually passed is a pointer to the function — but a pointer to a function is entirely different from a pointer to a member function.

    C++98/03 has a couple of adapters named mem_fun and mem_fun_ref that (sort of) deal with this.

    C++11 adds mem_fn and deprecates mem_fun and mem_fun_ref. It’s quite a bit easier to use, assuming you have a compiler new enough to include it.

    If your compiler is that new, however, it’ll probably also include lambdas, which can make the task considerably cleaner, because you can us an “in place” definition of a function object to handle the comparison:

    typedef std::pair<std::string, int> data_t;
    
    std::sort(score_list.begin(), score_list.end(),
        [](data_t const &a, data_t const &b) { 
            return a.second < b.second; 
        });
    

    If you Google for something like “C++11 lambda” you should find quite a bit more information about this (much of which will almost certainly lead directly back here to SO).

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

Sidebar

Related Questions

I am trying to use the .sort() function to sort a list so: <div
I'm trying to sort a vector of objects using a predicate function and I'm
I am trying to use the sort function to sort rows in a table
I'm trying to sort std::vector< std::vector< std::string> > perm; I am attempting to use
I am trying to use a sorting function for grade, which will sort name
I am trying to use a list which is passed to a function in
I'm trying to use the bubble sort method to sort an array of only
I am trying to use MPI to sort digits, after sorting by the different
I'm trying to use Twisted in a sort of spidering program that manages multiple
I am trying to use jQuery's draggable and sortable to sort elements listed inside

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.