srpax
srpax

Reputation: 21

For list sorting, is there a way to have multiple operator overloads with the same arguments?

Summary

I have a std::list of type Process*

class Process
{
// non essential stuff

// vars I want to sort by
int pid;
int burstTime;
int rBurstTime;
int priority;
}

I want to overload the < operator for sorting my list via list::sort()

bool operator<(Process const& p) {return this.priority < p.priority}
bool operator<(Process const& p) {return this.burstTime < p.burstTime}
// etc.

The above seems impossible since there is no way to determine the difference between the two (or am I on the right track?).

What I tried

I've tried something like

bool operator<(Process const& p, <k>) {return this.priority < p.priority}

where k is just any datatype/expected value that tells which overload to use, but this isn't possible since < overloads only take one argument.

Hopefully by now you can see what I am trying to do. Is there a C++ procedure for this that I am unaware of? I am a relatively new C++ programmer, so apologies if this is an easy fix.

Upvotes: 1

Views: 41

Answers (1)

srpax
srpax

Reputation: 21

Solved via Borgleader's comment:

std::list's sort can take a comparison function/functor, you should use that instead (this is also true of std::sort)

Upvotes: 1

Related Questions