Hello can anyone help me with sorting this struct by name and surname? Ive done like this and it doesn't sort it.

Thanks

Code:

#include <iostream>
#include <string>
#include <vector>
using namespace std;



struct student
{
    int st_studenta;
    string vpisna;
    string ime;
    string priimek;
};
student vpis(int st_studenta)
{
    student student;
    string vpisna,ime,priimek;
    cout<<endl<<"Vnesite vpisno številko študenta: ";
    cin>>vpisna;
    cout<<endl<<"Vnesite ime študenta: ";
    cin>>ime;
    cout<<"Vnesite priimek študenta: ";
    cin>>priimek;
    student.st_studenta=st_studenta+1;
    student.vpisna=vpisna;
    student.ime=ime;
    student.priimek=priimek;
    return student;
}
void izpis(student student)
{
    cout<<endl<<"St. študenta: "<<student.st_studenta;
    cout<<endl<<"Vpisna: "<<student.vpisna<<endl;
    cout<<endl<<"Ime: "<<student.ime<<endl;
    cout<<endl<<"Priimek: "<<student.priimek<<endl;
}

bool sort_by_name( const student & lhs, const student & rhs )
{
    return lhs.ime < rhs.ime;
}
bool sort_by_age( const student & lhs, const student & rhs )
{
    return lhs.priimek < rhs.priimek;
}

int main()
{
    int st_studentov=0;
    student seznam_student[50];
    int meni;
    do
    {
        cout<<"-------MENU-------------"<<endl;
        cout<<"________________________"<<endl;
        cout<<"1. Vnos študenta        "<<endl;
        cout<<"2. Izpis študentov      "<<endl;
        cout<<"3. Sortiranje študentov "<<endl;
        cout<<"4. Najdi študenta       "<<endl;
        cout<<"0. Izhod                "<<endl;
        cout<<"========================"<<endl;
        cin>>meni;
        
        if(meni==1)
        {
            seznam_student[st_studentov]=vpis(st_studentov);
            st_studentov++;
        }
        if(meni==2)
        {
            for(int a=0;a<st_studentov;a++)
            {
                izpis(seznam_student[a]);
            }
        }
        if(meni==3)
        {
            std::vector<student> people;
            std::sort( people.begin(), people.end(), sort_by_name );
            std::sort( people.begin(), people.end(), sort_by_age );
        }
    }while(meni!=0);
    return 0;
}

I do not know much about std::sort, but it is easy enough to sort this array manually. I would suggest using an insertion sort for each student entered, since it works really fast if the array is mostly sorted. Here is a sample implementation:

//This function will sort the vector of students
//It works REALLY fast on data that is mostly sorted
//  as such it should be called on the data every time
//  a new piece of data is entered.
void insertionSort(vector<student> &students, bool (*areInOrder)(student&,student&))
{
    for (int i=0; i<students.size(); ++i)
    {
        student thisStudent=students[i];//this will help make swapping faster
        int x=i-1;//this stores the index we want to swap to
        bool done=false;
        while (!done)
        {
            if (!areInOrder(student[x],students[i]))
            {
                //we will need to swap them
                students[x+1]=students[x];
                --x;
                if (x<0)
                {
                    //we must be done!
                    bone=true;
                }
            }
        }
        students[x+1]=thisStudent;
    }
}

Also the problem may be in your sort_by_name function. The std::string's > operator might not sort in ascending order, but may instead sort by location in memory. Perhaps you will have to wright your own? (compare ASCII values until you find a difference?)

>>sorting this struct by name and surname

If surname is listed first, like so:
Smith, John
then comparing the full string should sort by surname and then by first name.


>>student student;
That's not a good idea, it it's even legal. Never name an object the same name as the class name.

You do not populate the people vector.

This should work correctly, if memory serves:

std::sort(seznam_student,seznam_student+st_studentov,sort_by_name)

Or just use a vector from the beginning:

std::vector<student> seznam_student;

if(meni==1)
{
	seznam_student.push_back(vpis(st_studentov));
	st_studentov++;
}
if(meni==2)
{
for(int a=0;a<st_studentov;a++)
{
	izpis(seznam_student[a]);
}
}
if(meni==3)
{
	std::vector<student> people;
	std::sort( people.begin(), people.end(), sort_by_name );
	std::sort( people.begin(), people.end(), sort_by_age );
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.