Daniel Marques
Daniel Marques

Reputation: 526

Two overloading functions for the same operator and class

I want to use a sorting algorithm to sort my vector of clients, but the problem is I have two different criteria to sort them.

To display them to the screen and to save them into a file I need to sort them by ID, but to use it for some other stuff (like knowing the top ten worst clients) I need to sort them by the sum of money they've spent.

These are the overloading functions of the operator== for the client class, but obviously they can't co-exist. Can someone give me a solution for this?

    class Client
    {
    public:
    //...
        unsigned int getID() const;
        double getSum() const;
    //...
    private:
        unsigned int ID;
        //...
        double sum;
    };

    bool operator==(const Client &LHS, const Client &RHS)
    {
        return (LHS.getID() == RHS.getID());
    }

    bool operator==(const Client &LHS, const Client &RHS)
    {
        return (LHS.getSum() == RHS.getSum());
    }

Upvotes: 2

Views: 173

Answers (3)

Niall
Niall

Reputation: 30605

One of the std::sort function overloads takes a comparator, use that form and provide it with two independent functions or functors (or lambdas) for each instantiation.

class Client
{
public:
    //...
    unsigned int getID() const;
    double getSum() const;
    //...
private:
    unsigned int ID;
    //...
    double sum;
};

bool CompareByID(const Client &LHS, const Client &RHS)
{
    return (LHS.getID() < RHS.getID());
}

bool CompareBySum(const Client &LHS, const Client &RHS)
{
    return (LHS.getSum() < RHS.getSum());
}

// ...
std::sort(container.begin(), container.end(), CompareByID);

Note the sort requires a comparison that obeys its ordering requirements, usually it uses a less than comparison to order the elements. The exact comparison can be different, but needs to obey the same ordering requirements (for further reading, see the information for the std::sort algorithm, and this on strict weak ordering).

Upvotes: 5

clarkcox3
clarkcox3

Reputation: 589

Instead of relying on operator< to do your sorting, you can explicitly pass a comparator to std::sort:

std::vector<Client> vec;
...
auto sortByID = [](auto lhs, auto rhs){ return lhs.getID() < rhs.getID(); };
std::sort(vec.begin(), vec.end(), sortByID);
//vec is now sorted by ID

auto sortBySum = [](auto lhs, auto rhs){ return lhs.getSum() < rhs.getSum(); }
std::sort(vec.begin(), vec.end(), sortBySum);
//vec is now sorted by Sum

Upvotes: 0

Neeraj Kumar
Neeraj Kumar

Reputation: 1036

As suggested you could use std:sort algorithm. You need to create 2 function each for sum and Id comparison and pass them as function pointer for comparison.

bool compare_by_ID(const Client &LHS, const Client &RHS)
{
        return (LHS.getID() < RHS.getID());
}


bool compare_by_sum(const Client &LHS, const Client &RHS)
{
        return (LHS.getSum() < RHS.getSum());
}

and to use them you need to invoke sort in this way

vector<Client> v;  
// assume v contains list of clients.

// for comparison by sum
std:sort(v.begin(),v.end(),compare_by_sum);

// for comparison by Id
std:sort(v.begin(),v.end(),compare_by_ID);

Apart from comparison function you could use more sophisticated approach by using functors or function objects.

Upvotes: 0

Related Questions