The objective
I was trying to create a function that would compare 7 different integers. It should be able to be used to:
- Sort the two structures in a consistent manner
- Be able to detect if two structures are equal with
!isLessThan(a,b) && !isLessThan(b,a)
The code
At first, I thought of using a hash function to reduce all the values to one, but then it occurred to me that I was over-engineering this simple problem.
This is language agnostic, but I ended up doing it like this:
bool isLessThan(const MyStruct& lhs, const MyStruct& rhs) {
return
lhs.a +
lhs.b * 2 +
lhs.c * 3 +
lhs.d * 4 +
lhs.e * 5 +
lhs.f * 6 +
lhs.g * 7
<
rhs.a +
rhs.b * 2 +
rhs.c * 3 +
rhs.d * 4 +
rhs.e * 5 +
rhs.f * 6 +
rhs.g * 7;
}