Search code examples
c++visual-c++sortingbubble-sort

Having trouble sorting a file's contents in alphabetical order


I'm creating a program that sorts the contents of the file in order depending on the way the user wants it to be done. The file contains the student's last name, first name, gpa, and family income. I've gotten my program to sort based on the user's choice which includes last name, income, and gpa. My problem is that when the program sorts the file it ends up sorting specifically only the income, gpa, or last name. I want it to swap the whole line.

For example, I have 4 names below that show the last name, first name, gpa, and family income from left to right.

Hernandez Joshua 3.40 65000

Su Harry 3.33 60000

Tang Edward 4.00 100000

Guan Jessica 3.20 50000

After my program sorts the file by the last name it ends up only sorting the last name and not changing the rest of the data to accommodate where the last name is.

Guan Joshua 3.40 65000

Hernandez Harry 3.33 60000

Su Edward 4.00 100000

Tang Jessica 3.20 50000

Here is my Class Person

void getData(Person student[], int& item)
{
    ifstream fin;
    fin.open("C:students.txt");
    item = 0;

    while(!fin.eof())
    {
        fin >> student[item].lastName >> student[item].firstName >> student[item].gpa >> student[item].income;
        item++;
    }


}

void swap(string& name1, string& name2)
{
   //this is a swap function that swaps the data of the two string arguments.
   string temp;
   temp  = name1;
   name1 = name2;
   name2 = temp;
}

void swap2(float& num1, float& num2)
{
    //this is a swap function that swaps the data of the two float arguments
    float temp;
    temp = num1;
    num1 = num2;
    num2 = temp;
}

void sortByLastName(Person student[], int item)
{
   //This for loop will put the items in alphabetical order. 
   for(int j=0; j<item-1; j++)
   {
       //will perform the swapping until all items are in alphabetical order.
       for(int i=0; i<item-1; i++)
          //will swap the two items next to each other if the first item is bigger than the next item.
          if(student[i].lastName > student[i+1].lastName)
            swap(student[i].lastName, student[i+1].lastName);
   }
}

void sortByGpa(Person student[], int item)
{
    //This for loop will put the items in descending order.
   for(int j=0; j<item-1; j++)
   {
      //will perform the swapping until all items are in descending order.
      for(int i=0; i<item-1; i++)
         //will swap the two items next to each other if the first item is smaller than the next item.
         if(student[i].gpa < student[i+1].gpa)
            swap2(student[i].gpa, student[i+1].gpa);
   }
}

void sortByIncome(Person student[], int item)
{
   //This for loop will put the items in ascending order.
   for(int j=0; j<item-1; j++)
   {
      //will perform the swapping until all items are in descending order.
      for(int i=0; i<item-1; i++)
         //will swap the two items next to each other if the first item is smaller than the next item.
         if(student[i].income < student[i+1].income)
            swap2(student[i].income, student[i+1].income);
   }
}

void getChoice(int choice, Person student[], int item)
{

    cout << "Press 1 to sort by last name. Press 2 to sort by gpa. Press 3 to sort by income.";
    cin >> choice;

    if(choice == 1)
        sortByLastName(student, item);
    else if(choice == 2)
        sortByGpa(student, item);
    else if(choice == 3)
        sortByIncome(student, item);
}

void output(Person student[], int item)
{
   //Displays all of the names to the screen.
   for(int i=0; i<item; i++)
      cout << student[i].lastName << " " << student[i].firstName << " " << student[i].gpa << " " << student[i].income << endl;
}

Solution

  • Think of the simple example of two students.

    You start with an array of students: [Student 1, Student 2]

    The goal is to order the students in this array by some property of the student.

    Lets say we want to order the students by their gpa.

      if (student1.gpa > student2.gpa) 
        put student 1 first
      otherwise
        put student 2 first
    

    As student 1 is already first, no change is needed. To put student 2 first we want the array to look like: [Student 2, Student 1]

    One way to do this is:

    Person temp = student 1;
    array[0] = student 2;
    array[1] = student 1;
    

    Above you generalized the swap function so that you swap two pointers. This can be done for a person as well.

    swapPeople(array[0], array[1])

    void swapPeople(Person &a, Person &b) {
    Person temp = a;
    b = a;
    a = temp;
    }