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

The Archive Base Latest Questions

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

I’m trying to create a function to sort a list of contacts in an

  • 0

I’m trying to create a function to sort a list of contacts in an address book by name or last name.

 void sortList (list<Contact> & address_book){

 //define two iterators - first to point to the first element from the list, second to the second element
 list<Contact>::iterator it = address_book.begin();
 list<Contact>::iterator it2 = address_book.begin();
 it2++;

 //get the last name for the first 2 contacts
 string last_name1 = it->get_last_name();
 string last_name2 = it2->get_last_name();

 int i = 0;

 while (i < last_name1.length() && i < last_name2.length()){

       if (last_name1[i] < last_name2[i]){
          swap(it, it2);
          break;
       }

 }
}

I’m sure I’m not doing it correctly, but I’m a little bit lost with these iterators. I also know I should have another while loop to loop through all my contacts until all of them are sorted but, honestly I have no idea how to implement it.

  • 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-30T09:10:26+00:00Added an answer on May 30, 2026 at 9:10 am

    std::list has an overloaded member function sort, that

    Sorts the elements in ascending order. The order of equal elements is guaranteed to be preserved. The first version uses operator< to compare the elements, the second version uses the given comparison function comp.

    To give the comparison function you can use functors:

    struct sort_by_name {
      bool operator()(const Contact &a, const Contact &b)
      { return a.get_name() < b.get_name(); }
    };
    struct sort_by_last_name {
      bool operator()(const Contact &a, const Contact &b)
      { return a.get_last_name() < b.get_last_name(); }
    };
    

    or simpler free functions

    bool cmp_by_name(const Contact &a, const Contact &b)
    { return a.get_name() < b.get_name(); }
    bool cmp_by_last_name(const Contact &a, const Contact &b)
    { return a.get_last_name() < b.get_last_name(); }
    

    and you call it either

     address_book.sort(sort_by_name());
     address_book.sort(sort_by_last_name());
    

    or

     address_book.sort(cmp_by_name);
     address_book.sort(cmp_by_last_name);
    

    the accessors get_name() and get_last_name() must be const.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
I am reading a book about Javascript and jQuery and using one of the
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to render a haml file in a javascript response like so:

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.