If I had a class Cell
, for example, which I want to sort according to the following function (x
and y
here being int
member variables with accessors):
bool sortByCoordinates(const Cell& c1, const Cell& c2) {
return c1.getX() < c2.getX() || (c1.getX() == c2.getX() && c1.getY() < c2.getY());
}
Where exactly is the best place to put this function so that I can use it with a functions such as std::sort
?
In the examples they just have the method floating in the source file above where it is needed, but in practice I want to keep it associated with the Cell
class. I know that I could override the operator<
but there might be other sort methods by which I'd like to sort Cell
, and I'm not a big fan of overriding operators for code clarity's sake anyhow.
At the moment I have it as a static
method in my Cell.h
file, so that I can call it in when sorting like so:
std::sort(cells.begin(), cells.end(), Cell::sortByCoordinates);
Is this the best practice for multiple (or even singular) custom sort functions, and is the header file the right place for them? If not, what is?