Does anyone have a useful mnemonic for implementing Comparator?
Asked Answered
A

5

16

Every time I need to implement a comparator, I get stuck trying to remember when I should return -1 and when 1, and I have to look it up.

I mean, obviously -1 is less, so it implies that first is less than second. But whenever I say that to myself, I get that nagging "are you sure?" feeling. I suspect part of my confusion comes from implementing it the other way around whenever I need a descending sort.

What do you use to remember which is which?

Aquavit answered 30/3, 2011 at 13:56 Comment(4)
This could be even language-agnostic.Devote
tuff question - I just remember it by hearth :)Shatterproof
I predict that now that you've taken the time and effort to write this as a question on this site, you will find out that you no longer have a problem remembering what values you should return. :-DAudubon
Can you clarify in your quesiton whether you are looking for a ascending or descending result?Unhair
M
6

comparator.compare(a, b) < 0 <==> a < b

Medrano answered 30/3, 2011 at 14:18 Comment(1)
For some reason I like this one the best.Aquavit
N
11

I use this simple "substraction" mnemonic:

first - second

So, if first is "less" than second you'll get negative result, otherwise - positive or zero if they are equal.

Novocaine answered 30/3, 2011 at 14:0 Comment(3)
+0: Unless overflow is a problem in which case Integer.MIN_VALUE - Integer.MAX_VALUE = 1 ;)Polley
or the ever so golden (0 - Integer.MIN_VALUE) == Integer.MIN_VALUE. :-)Artisan
My mind uses Doubles. That's why there is no equality for me and I do usually forget a lot of details :)Novocaine
M
6

comparator.compare(a, b) < 0 <==> a < b

Medrano answered 30/3, 2011 at 14:18 Comment(1)
For some reason I like this one the best.Aquavit
G
5

I am not sure what you mean by mnemonic. However, I have had a very similar cognitive dissonance.

I am very visual, so I use the number line (the one I was taught in grade school). I just visualize the negative numbers as "left", 0 as "center" and positive numbers as "right". That the corresponds to the truth: -1 < 0 < 1

Gilder answered 30/3, 2011 at 14:2 Comment(0)
S
2

I remember the base integer case (pseudocode):

int comparator(int a, int b) {
   return a-b;
}

So if we give in a small a and a large b which is the first < last we get a negative result.

I have a more visual memory, so remembering the "structure" of that function is easy and natural for me.

Surrogate answered 30/3, 2011 at 14:2 Comment(0)
T
0

I used to always check the documentation when implementing Comparator and Comparable interfaces.

Question: Compare a and b
Lets first at look ascending order since the descending order will be just the inverse of whatever we do.
Question can be translated to given two numbers a and b, how would you put them on the number line?
if a < b, then we will put a on the negative side and b on the positive side.
else if a = b then we will put both at the center (at 0)
otherwise b will be on the negative side and a will be on the positive side.

enter image description here

Comparator Implementation:
Here you are comparing a to b.

 @Override
    public int compare(MyClass a, MyClass b) { //always good to rename your variables like this to be consistent 
        return a.f1 - b.f1; 
    }

Comparable Implementation:
Here you are comparing this to other.

@Override
    public int compareTo(MyClass other) { // this is same as compare(this, other)
        return this.f1 - o.f1;
    }
Traynor answered 22/12, 2020 at 18:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.