I am asked to sort person objects by name and by salary In m

I am asked to sort person objects by name and by salary. In my code, I am having trouble sorting
the names and salary
. When my salary sorts, it appears to be only checking the first number and
I am not sure what is going on with the names.
This is the code I have so far.

#include <iostream>
#include <iomanip>
#include <string>
#include \"Person.h\"

using namespace std;

bool order_N_or_S = false;

int main() {
   void bsort(Person **, int, bool);
   Person *perPtr[80];
   int n = 0;
   string name;
   float salary;
   char choice;
   //input person
   do {
       perPtr[n] = new Person; //allocating memory
       cout << \"Input name:\" << endl;
       getline(cin, name);
       cout << \"Input salary:\" << endl;
       cin >> salary;
       perPtr[n]->setPerson(name, salary);
       cout << \"Person has been created.\" << endl;
       cout << \"Enter another (y / n)?\" << endl;
       cin >> choice;
       cin.ignore();
       n++;
   } while (choice == \'y\');

   //display unsorted Person objects
   cout << \"---------Unsorted list-----------\" << endl;
   for (int i = 0; i < n; i++) {
       perPtr[i]->print();
       cout << endl;
   }
   //displays person objects sorted by name
   bsort(perPtr, n, true);
   cout << \"--------Ordered by Name----------\" << endl;
   for (int i = 0; i < n; i++) {
       perPtr[i]->print();
       cout << \"----------------------------------\" << endl;
   }
   cout << \"\ \ \";

   //displays person objects sorted by salary
   bsort(perPtr, n, false);
   cout << \"--------Ordered by Salary---------\" << endl;
   for (int i = 0; i < n; i++) {
       perPtr[i]->print();
       cout << \"----------------------------------\" << endl;
   }
   system(\"pause\");
   return 0;
}

//Will sort person either by name or salary, depending on bool value
void bsort(Person ** perPtr, int n, bool s) {
   void order(Person **, Person **);

   order_N_or_S = s;
   int k = 0;
   for (int j = 0; j < n - 1; j++) {
       for (j + 1; k < n; k++) {
           order(perPtr + j, perPtr + k);
       }
   }

}

//Sorts in either name or salary
void order(Person **p1, Person **p2) {
   if ((*p1)->getName() > (*p2)->getName() && order_N_or_S == true) {
       Person *temp = *p1;
       *p1 = *p2;
       *p2 = temp;
   }
   if ((*p1)->getSalary() > (*p2)->getSalary() && order_N_or_S == false) {
       Person *temp = *p1;
       *p1 = *p2;
       *p2 = temp;
   }
}


Output

---------Unsorted list-----------
Name: Person A
Salary $12.3

Name: Person B
Salary $4.5

Name: Person F
Salary $12345.7

Name: Person D
Salary $6.77

Name: Person O
Salary $0.023

--------Ordered by Name----------
Name: Person A
Salary $12.3
----------------------------------
Name: Person B
Salary $4.5
----------------------------------
Name: Person F
Salary $12345.7
----------------------------------
Name: Person D
Salary $6.77
----------------------------------
Name: Person O
Salary $0.023
----------------------------------


--------Ordered by Salary---------
Name: Person O
Salary $0.023
----------------------------------
Name: Person A
Salary $12.3
----------------------------------
Name: Person F
Salary $12345.7
----------------------------------
Name: Person D
Salary $6.77
----------------------------------
Name: Person B
Salary $4.5
----------------------------------

Solution

Try chahing this method:


void bsort(Person ** perPtr, int n, bool s) {
   void order(Person **, Person **);

   order_N_or_S = s;
   int k = 0;
   for (int j = 0; j < n - 1; j++) {
       for (k=j + 1; k < n; k++) {
           order(perPtr + j, perPtr + k);
       }
   }

}

I am asked to sort person objects by name and by salary. In my code, I am having trouble sorting the names and salary. When my salary sorts, it appears to be on
I am asked to sort person objects by name and by salary. In my code, I am having trouble sorting the names and salary. When my salary sorts, it appears to be on
I am asked to sort person objects by name and by salary. In my code, I am having trouble sorting the names and salary. When my salary sorts, it appears to be on

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site