Sort names alphabetically

41,108

Solution 1

Here's an answer using std::sort. I changed some of your C-like approach and using std::sort actually forces me to do it. The comparison function(compareStudents) needs objects so I had to create the struct. Vector has been used for the same reason although it would have been possible to keep using arrays but that's generally frowned upon.

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

struct Student {
    string name;
    float gpa;

    Student(string name, float gpa) {
        this->name = name;
        this->gpa = gpa;
    }
};

bool compareStudents(Student a, Student b) {
    return a.name.compare(b.name) < 0;
}

int main() {
    const int studentCount = 2;
    vector<Student> studentVector;
    int i;

    for (i = 0 ; i < studentCount ; i++) {
        cout << "Enter name " << i + 1 << "  :   ";
        string name;
        cin >> name;
        cout << "Enter GPA     :   ";
        float gpa;
        cin >> gpa;
        cout << endl;

        studentVector.push_back(Student(name, gpa));
    }

    cout << "\n********** Your entered data **********\n\n";

    cout << "\tName" << "\t\t" << "GPA\n\n";

    vector<Student>::iterator it = studentVector.begin();
    for (; it != studentVector.end(); ++it) {
        Student student = *it;
        cout << "\t" << student.name << "\t\t" << student.gpa;
        cout << endl;
    }

    sort(studentVector.begin(), studentVector.end(), compareStudents);

    cout << "\n\n******* Sorted data (w.r.t name) *******\n\n";

    cout << "\tName" << "\t\t" << "GPA\n\n";

    it = studentVector.begin();
    for (; it != studentVector.end(); ++it) {
        Student student = *it;
        cout << "\t" << student.name << "\t\t" << student.gpa;
        cout << endl;
    }

    cout << endl;

    return 0;
}

Solution 2

If you convert the names' character to upper-case using std::toupper you should then just be able to compare the the strings using < operator.

Edit: if you don't want to use std::sort :-)

Share:
41,108
Silver Falcon
Author by

Silver Falcon

Updated on February 22, 2020

Comments

  • Silver Falcon
    Silver Falcon over 4 years

    I'm trying to sort names alphabetically e.g If user enters names and GPA:

    Names          GPA
    Peter          2.8
    Robert         5.6
    David          7.8
    

    The output should be : -

    Names          GPA
    David          7.8
    Peter          2.8
    Robert         5.6
    

    Here is my program so far (INCOMPLETE):-

    #include <iostream>
    using namespace std;
    
    int main()
    {
        char name [5][25];
        float gpa [5];
        int i;
    
        for (i=0 ; i<5 ; i++)
        {
            cout << "Enter name " << i+1 << "  :   ";
            cin >> name [i];
            cout << "Enter GPA     :   ";
            cin >> gpa [i];
            cout << endl;
        }
    
        cout << "\n********** Your entered data **********\n\n";
    
        cout << "\tName" << "\t\t" << "GPA\n\n";
    
        for (i=0 ; i<5 ; i++)
        {
            cout << "\t" << name [i] << "\t\t" << gpa [i];
            cout << endl;
        }
    
        for (i=0 ; i<5 ; i++)
        {
            for (int j=0 ; j<1 ; j++)
            {
                cout << (int) name [i][j] << endl;
    
    
    
            }
        }
    
        cout << "\n\n******* Sorted data (w.r.t name) *******\n\n";
    
        cout << "\tName" << "\t\t" << "GPA\n\n";
    
        for (i=0 ; i<5 ; i++)
        {
            cout << "\t" << name [i] << "\t\t" << gpa [i];
            cout << endl;
        }
    
        cout << endl;
    
        return 0;
    }
    

    Remember, only name should be sorted alphabetically. I have taken the ASCII values of the first characters of entered names in the middle for loop but:- 1- ASCII code for 's' is not the same as 'S' (That's a problem for me) 2- I can't seem create a logic to compare the ASCII values of the first letters of names then sort them accordingly. Then afterwards linking the name with the sorted letter list and displaying the result. Also the GPA should be linked with the names.

    Any help would be appreciated.

  • Silver Falcon
    Silver Falcon over 10 years
    Yes 'sort' is complex for me. But can you provide any reference on how can I use this?
  • splrs
    splrs over 10 years
    And to compare the strings you can just use "string1 < string2".
  • Silver Falcon
    Silver Falcon over 10 years
    Well actually I'm using 2D character array for names. So you mean with toupper, I can convert a lower-case input to upper-case internally? And then I should compare the ASCII values of the letters? Or can I just compare the first character of name?
  • Silver Falcon
    Silver Falcon over 10 years
    Sorry but please can you edit and replace the vector and sorting portion with logical algorithms using loops etc? I haven't studied these yet and its difficult for me to understand.
  • splrs
    splrs over 10 years
    I mean you can literally compare them by going name[0] < name[1]. Create a couple of temp variables (char n1[5]; char n2[5]) and copy the chars from your original arrays 1-by-1 e.g. nm1[j] = name[i][j], then n1 < n2 will compare them.
  • VIRTUAL-VOID
    VIRTUAL-VOID over 10 years
    @denarced . Nice work. you wrote ........Student student = *it; ......did you mean ....const Student & student = *it; ...?
  • denarced
    denarced over 10 years
    No, but that's probably correct :) I wrote the code with minimal attention to details because only the basic idea was important. I haven't been dealing with C++ lately.
  • denarced
    denarced over 10 years
    @HamzaUmar, if you're asking me to basically rewrite std::sort then no, I won't do that. I in fact have never wrote any kind of proper sorting algorithm because quicksort (as an example) already exists and it's not necessary for programmers to know its intricate details. I'd be happy to answer any questions you have in order to clarify the current code thou.