Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I need a program in C++ that uses a generic sort function to do the follo

ID: 3816789 • Letter: H

Question

Hello, I need a program in C++ that uses a generic sort function to do the following:

(i) sort numbers ascending by numerical value,

(ii) sort people alphabetically (lexicographically) by name, and to

(iii) sort people descending by age, where people of the same age should be sorted alphabetically (lexicographically).

The point here is to use the same function to do all 3 different sort operations. Try to reuse as much of your code and focus on clarity and brevity.

Data to use to sort:

The sequence of floating point numbers: (645.32, 37.40, 76.30, 5.40, -34.23, 1.11, -34.94, 23.37, 635.46, -876.22, 467.73, 62.26)

The following sequence of people with name and age of each person. The name is a string and the age an integer: (Hal, 20; Susann, 31; Dwight 19; Kassandra, 21; Lawrence, 25; Cindy, 22; Cory, 27; Mac, 19; Romana, 27; Doretha, 32; Danna, 20; Zara, 23; Rosalyn, 26; Risa, 24; Benny, 28; Juan, 33; Natalie, 25)

Please use both of the data above in the answer for the correct answer.

This has to be in C++ --Thanks

Explanation / Answer

/*

Explanation of answer:

The key here is to write the comparator function, which determines the order of sorting.
We have a generic sort function in the C++ library.
The function takes 2 parameters (start of array, last location of array) and an optional third parameter which
controls the ordering of element.

For sorting the numbers in ascending order, we require only 2 parameters.
For sorting the people by name, we need to proovide 3rd parameter(a comparator function) which compares 2 string.( sortByName: used here)
For sorting the people first by descending order of age and alphabetically if age is same, we again need a comparator function
which determines the order. ( sortbyNameAge : has been used here)

*/
#include <iostream>
#include <string>
#include<bits/stdc++.h>

using namespace std;

struct Person
{
string name;
int age;
};

bool sortByNameAge(Person P1, Person P2)
{
// aphabetical order for name and descending order for age
if(P1.age == P2.age)
return P1.name < P2.name;
else
return P1.age > P2.age;
}


bool sortByName(Person P1, Person P2)
{
if(P1.age == P2.age)
return P1.name < P2.name;
}

// sample program
int main()
{

int count = 0;

cout << "No of people: ";
cin >> count;
  
Person people[count];
  
for (int i = 0; i < count; i++)
{
cout << "Person " << i + 1 << " name: ";
cin >> people[i].name;

cout << "Person " << i + 1 << " age: ";
cin >> people[i].age;
}

// sorting by comparing name and age
sort(people,people + count,sortByNameAge);

// sorting by name
sort(people,people + count,sortByName);


//sorting numbers
//sort(start,end);

for(int i=0; i < count ; i++){
cout << "Person" << i+1 << "Name:" << people[i].name << " Age:" << people[i].age <<endl;
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote