henry leu wrote:. . .

Positive: swap the element

Negative: no swap

Zero: no swap

I am not convinced that is right. I think it would be more like what follows, but I think even that isn't accurate.

If obj1 should APPEAR BEFORE obj2 in a sorted list, then we return a negative number. (No swap)

If obj1 should APPEAR AFTER obj2 in a sorted list, then we return a positiv default e number. (Swap)

If we don't care which object comes first (because they might be equal), then we return zero. (no swap)

. . .

A Comparator (as does the Comparable interface) imposes an

__ordering__ on objects of a particular type. The

word “ordering” is used in the sense the mathematicians would use is.

1: If you get a positive result from` compare(x,y)`, then *x* is regarded as __larger__ than *y*.2: If you get a negative result from` compare(x,y)`, then *x* is regarded as __smaller__ than *y*.3: If you get a zero result from` compare(x,y)`, then *x* is regarded as __the same size__ as *y*.There is an explanation of how Java® implements ordering in the

Java™ Tutorials. That link tells you about Comparator and Comparable, and the many errors which can beset naïve attempts at ordering.

If you reember that sorting is usually implemented in ascending order, you will have to redefine the comparison so you get the sign different.

1: Ascending order: make "Campbell" smaller than "Henry"2: Descending order: make "Campbell" larger than "Henry"By writing that Comparator, you are defining a different ordering for names. That doesn't make the names any different, but your definition of which order you want them in is different.

Don't think about, “don't care,” please. We do care. Nor, “appear before”. Think “larger and smaller, as I am defining it at present.” The sorting algorithms will then interpret the result from comparison methods like this:-

1: Negative: in the desired order: has already been sorted.2: Positive: in the wrong order: sorting required.3: Zero result: the two are “the same”: mustn't be altered.Because comparisons are so error‑prone, several

` static `methods were added to Comparator in Java8. It is now much more reliable to write

than

My second code block shows very unreliable code, which can be relied on to produce incorrect results in many cases. That is an example showing that you are usually better using one of the methods of Comparator than writing your own comparison method like that. The comparison method you showed in your second post is similarly incorrect.

If you look through the

methods of Comparator, you will find two which change “smaller” to “larger” and

*vice versa*.

I think this question is no longer certification‑specific, so I shall add you to a more general forum.