Sorting vectors, that contains class objects, in C++ by class member's in order

What I aim is that I would like to sort the vector by following criteria; I sorted the whole vector by "name" and when "name"s are equal, I want it to sort by "surname" etc. Here is the following code:

Below I created a class;

class NotLearning {
public:
    NotLearning(); // Constructor
    int getID()const; // Function to used to initialise private variable "ID" of the class via the constructor.
    int getDay()const; // Function to used to initialise private variable "day" of the class via the constructor.
    int getYear()const; // Function to used to initialise private variable "year" of the class via the constructor.

    double getAverage()const; // Function to used to initialise private variable "average" of the class via the constructor.

    std::string getName()const; // Function to used to initialise private variable "name" of the class via the constructor.
    std::string getSurname()const; // Function to used to initialise private variable "surname" of the class via the constructor.
    std::string getCity()const; // Function to used to initialise private variable "city" of the class via the constructor.
    std::string getMonth()const; // Function to used to initialise private variable "month" of the class via the constructor.

private:
    int ID;
    int day;
    int year;

    double average;

    std::string name;
    std::string surname;
    std::string city;
    std::string month;
};

I sort the vector, that consists of class objects, by the class member of "ID" with the following code:

void sortVectorByID(std::vector<NotLearning>& newClass)
{
    std::sort(newClass.begin(), newClass.end(), cmp_by_id);
}

In which "cmp_by_id" define like that;

bool cmp_by_id(const NotLearning& one, const NotLearning& two) // compare classes inside the vector by checking the member of "ID".
{
    return one.getID() < two.getID();
}

So far I have sorted the vector successfully. After that, I implemented the following code for my purpose;

void sortVectorByDetail(std::vector<NotLearning>& newClass)
{
    std::sort(newClass.begin(), newClass.end(), cmp_by_detail);
}

Where "cmp_by_detail" defined like that;

bool cmp_by_detail(const NotLearning& one, const NotLearning& two) // compare classes inside the vector by checking the members in order.
{
    bool compare_result = cmp_by_id(one, two);
    if (compare_result)
        return compare_result;

    compare_result = cmp_by_average(one, two);
    if (compare_result)
        return compare_result;

    compare_result = cmp_by_name(one, two);
    if (compare_result)
        return compare_result;

    compare_result = cmp_by_surname(one, two);
    if (compare_result)
        return compare_result;

    compare_result = cmp_by_city(one, two);
    if (compare_result)
        return compare_result;

    compare_result = cmp_by_day(one, two);
    if (compare_result)
        return compare_result;

    compare_result = cmp_by_month(one, two);
    if (compare_result)
        return compare_result;

    compare_result = cmp_by_year(one, two);
    if (compare_result)
        return compare_result;

    return true;
}

I did not give all codes in the class, just in order not to distract you. However, I leave GitHub here if you wanted to see all codes.

I hope I could tell you what I need.

I thank you deeply for sparing your time if you came to this line ^^.

Have a nice day.

Edit: I forgot to mention that I get an error that says debug assertion failed invalid comparator when I called sortVectorByDetail() function. I also below leave the way I used to call these functions;

#include "LearningCpp.h"


int main()
{
    srand((unsigned int)time(NULL));

    std::vector<NotLearning> myClass;
    fillVector(myClass);
    showVector(myClass);

    sortVectorByDetail(myClass);
    showVector(myClass);  
}

Edit: I now solved my problem with following implementation to the function of cmp_by_detail. Code is here;

bool cmp_by_detail(const NotLearning& one, const NotLearning& two) {

    if (one.getID() != two.getID())
    {
        return one.getID() < two.getID();
    }

    if (one.getAverage() != two.getAverage())
    {
        return one.getAverage() < two.getAverage();
    }

    if (one.getName() != two.getName())
    {
        return one.getName() < two.getName();
    }

    if (one.getSurname() != two.getSurname())
    {
        return one.getSurname() < two.getSurname();
    }

    if (one.getCity() != two.getCity())
    {
        return one.getCity() < two.getCity();
    }

    if (one.getDay() != two.getDay())
    {
        return one.getDay() < two.getDay();
    }

    if (one.getMonth() != two.getMonth())
    {
        return one.getMonth() < two.getMonth();
    }

    if (one.getYear() != two.getYear())
    {
        return one.getYear() < two.getYear();
    }

    return true;
}

Upvotes: 0

Views: 188

Answers (3)

G.M.
G.M.

Reputation: 12899

If you want to compare certain 'properties' of two instances of the same class lexicographically you can make use of std::forward_as_tuple. With that your cmp_by_detail implementation becomes...

bool cmp_by_detail (const NotLearning &one, const NotLearning &two)
{
    return std::forward_as_tuple(one.getID(), one.getAverage(), one.getName(),
                                 one.getSurname(), one.getCity(), one.getDay(),
                                 one.getMonth(), one.getYear())
         < std::forward_as_tuple(two.getID(), two.getAverage(), two.getName(),
                                 two.getSurname(), two.getCity(), two.getDay(),
                                 two.getMonth(), two.getYear());
}

Upvotes: 0

Arpit Jain
Arpit Jain

Reputation: 27

Implement one function pointer for sort function.

bool cmp(NotLearing a, NotLearning b)
{
    if(a.getName()==b.getName())
       return a.getSurname()<b.getSurname();
    return a.getName<b.getName();
}

then sort your vector

vector<NotLearning> v;
sort(v.begin(),v.end(),cmp); 

Upvotes: 1

Deep Raval
Deep Raval

Reputation: 407

I think you should check all function which you are using in cmp_by_detail. Somewhere in function assertion must be failing. Take a look at this for more info

Upvotes: 0

Related Questions