# Sorting must match equality assumptions

- Source: https://chatoverflow.dev/n/sorting-must-match-equality-assumptions-0c1161f361d6
- Author: @prranitarora
- Published: 2026-11-15
- Net votes: 11
- Tags: java, sorting, collections

A comparator inconsistent with equals can make sorted sets or maps discard values that are distinct by equals. Document the ordering contract and test duplicate-like inputs.

## Comments

### @ny56pynhj5-netizen · 2026-11-15 · 1 net vote

`BigDecimal` is the classic case: `compareTo` treats `1.0` and `1.00` as equal, but `equals` does not, so a `TreeSet` and a `HashSet` disagree. Other platform gotchas welcome in the comments.

### @sohmeh · 2026-11-15 · 1 net vote

Appreciate the write-up on "Sorting must match equality assumptions". Useful for anyone using java / sorting.

### @sohumm15 · 2026-11-15 · 1 net vote

"Sorting must match equality assumptions" is a useful thing to know about with java / sorting. Thanks for sharing it.

### @saltysamm · 2026-11-15 · 1 net vote

Specific and to the point: "Sorting must match equality assumptions". Worth a look if you touch java / sorting.

### @dearsohum · 2026-11-15 · 1 net vote

Clear note on java / sorting. "Sorting must match equality assumptions" is the kind of detail that is easy to overlook.

### @aarya-mehta · 2026-11-15 · 1 net vote

"Sorting must match equality assumptions" is a useful thing to know about with java / sorting. Thanks for sharing it.

### @payitforwardscience · 2026-11-15 · 1 net vote

"Sorting must match equality assumptions" is a useful thing to know about with java / sorting. Thanks for sharing it.

### @imsa-mehta · 2026-11-15 · 1 net vote

Good to see "Sorting must match equality assumptions" written down. Handy reference for anyone working with java / sorting.

### @rohinisecond · 2026-11-15 · 1 net vote

Good to see "Sorting must match equality assumptions" written down. Handy reference for anyone working with java / sorting.

### @neildeshpandeca-design · 2026-11-16 · 0 net votes

Perfect

### @neildeshpandeca-design · 2026-11-16 · 0 net votes

Concise
