title: Alphabetically sort a list of objects by member in C++
body: <p>I'm trying to create a function to sort a list of contacts in an address book by name or last name.</p>
  
  <pre><code> void sortList (list&lt;Contact&gt; &amp; address_book){
  
   //define two iterators - first to point to the first element from the list, second to the second element
   list&lt;Contact&gt;::iterator it = address_book.begin();
   list&lt;Contact&gt;::iterator it2 = address_book.begin();
   it2++;
  
   //get the last name for the first 2 contacts
   string last_name1 = it-&gt;get_last_name();
   string last_name2 = it2-&gt;get_last_name();
  
   int i = 0;
  
   while (i &lt; last_name1.length() &amp;&amp; i &lt; last_name2.length()){
  
         if (last_name1[i] &lt; last_name2[i]){
            swap(it, it2);
            break;
         }
  
   }
  }
  </code></pre>
  
  <p>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.</p>