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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:53:38+00:00 2026-05-26T18:53:38+00:00

I want to have a stl list of objects where each object contains two

  • 0

I want to have a stl list of objects where each object contains two int‘s.
Afterwards I want to sort the list with stl::sort after the value of the first int.
How do I tell the sort function that it’s supposed to sort after the first int?

  • 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-26T18:53:38+00:00Added an answer on May 26, 2026 at 6:53 pm

    You can specify a custom sort predicate. In C++11 this is best done with a lambda:

    typedef std::pair<int, int> ipair;
    std::list<ipair> thelist;
    
    thelist.sort([](const ipair & a, const ipair & b) { return a.first < b.first; });
    

    In older versions of C++ you have to write an appropriate function:

    bool compFirst(const ipair & a, const ipair & b) { return a.first < b.first; }
    
    thelist.sort(compFirst);
    

    (Instead if ipair you can of course have your own data structure; just modify the comparison function accordingly to access the relevant data member.)

    Finally, if this makes sense, you can also equip your custom class with an operator<. That allows you to use the class freely in any ordered context, but be sure to understand the consequences of that.

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

Sidebar

Related Questions

If I have a list<object*>>* queue and want to pop the first object in
If i have a stl map from string to int and i want to
I have multiple threads modifying an stl vector and an stl list. I want
If I have two stl vectors vect1, vect2 and I want to produce from
I have a very basic question. I want to use STL's list instead of
I have two STL containers that I want to merge, removing any elements that
Suppose I have a STL map where the values are pointers, and I want
I have a UITextView and I want have two buttons. When the user taps
I want to have two button in the same size one in the left
I have a STL container full of billions of the following objects pair<SomeClass*, SomeClass*>

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.