I need to sort a list of first and last names. My names are stored in pairs with
ID: 3847472 • Letter: I
Question
I need to sort a list of first and last names. My names are stored in pairs with first being the first name and second being the last name. Example:
make_pair("Josh", "Nahum")
I want to order by the second attribute (then ordering by the first attribute if the second attribute values are the same).
Example Sorted:
Hayam Abdelrahman
Doug Kirkpatrick
Josh Nahum
Zach Nahum
Charles Ofria
Write a function (named "sort_by_second") that takes a two const_iterators from a vector of pairs of string key and string value. The first iterator points at the first element, and the second iterator points at one past the last element (the end iterator).
It should return a new vector of pairs that is sorted according to the above specification. Do not change the original vector.
Starter Code:
use c++
Explanation / Answer
Here is the implementation of the function and its usage in main function. Output is shown below. Please don't forget to rate the answer if it helped. Thank you very much.
#include <vector>
using std::vector;
#include <string>
using std::string;
#include <iostream>
using std::cout;
using std::endl;
using Name = std::pair<string, string>;
//compares 2 names and returns -1 if n1 < n2, 0 if n1 = n2 and 1 if n1 > n2. Comparison is based on second
//attribute and if second are equal then compare first attribute
int compare(Name n1, Name n2)
{
if(n1.second < n2.second)
return -1;
else if(n1.second > n2.second)
return 1;
else //second are equal , compare by first
{
if(n1.first < n2. first)
return -1;
else if(n1.first > n2.first)
return 1;
else
return 0;
}
}
vector<Name> sort_by_second(
const vector<Name>::iterator & begin,
const vector<Name>::iterator & end) {
vector<Name> list(begin, end);
int n = list.size(), minIdx;
//implemnet selection sort algorithm to sort
for(int i = 0; i < n; i++)
{
minIdx = i;
for(int j = i+1 ; j < n; ++j)
{
if(compare(list[j] , list[minIdx]) < 0)
minIdx = j;
}
if(minIdx != i)
{
//swap
Name temp;
temp = list[i];
list[i] = list[minIdx];
list[minIdx] = temp;
}
}
return list;
}
//displays the vector of Name
void display(vector<Name> list)
{
for(int i = 0; i < list.size(); i++)
{
cout << list[i].first << " " << list[i].second <<endl;
}
cout << endl;
}
int main()
{
vector<Name> unsorted;
unsorted.push_back( std::make_pair("Charles", "Ofria"));
unsorted.push_back( std::make_pair("Zach", "Nahum"));
unsorted.push_back( std::make_pair("Hayam", "Abdelrahman"));
unsorted.push_back( std::make_pair("Doug", "Kirkpatrick"));
unsorted.push_back( std::make_pair("Josh", "Nahum"));
cout << "unsorted list is " << endl;
display(unsorted);
vector<Name> sorted = sort_by_second(unsorted.begin(), unsorted.end() );
cout << "sorted list is " << endl;
display(sorted);
}
output
unsorted list is
Charles Ofria
Zach Nahum
Hayam Abdelrahman
Doug Kirkpatrick
Josh Nahum
sorted list is
Hayam Abdelrahman
Doug Kirkpatrick
Josh Nahum
Zach Nahum
Charles Ofria