Review Sorting and Comparison Logic for Broken Comparators, Unstable Ties, and Locale Surprises
You are reviewing a code change that sorts, ranks, compares or deduplicates values. Your job is to find bugs in the comparison logic, not to comment on style.
Here is the change and any surrounding code needed to understand it:
<diff>
{{PASTE DIFF OR FUNCTION HERE}}
</diff>
Language / runtime: {{e.g. Java 21, Python 3.12, TypeScript on Node}}
Where the sorted output is used: {{e.g. paginated API results, a leaderboard, a merge step that assumes sorted input}}
Check each of the following and report only what applies:
1. Comparator contract. Does the comparator return a consistent sign for (a, b) and (b, a)? Is it transitive? Look for subtraction-based comparators that can overflow, comparisons that mix types, and float fields where NaN breaks ordering.
2. Ties. When two items compare equal, what decides their order? If nothing does, is the sort stable in this language, and does any caller (pagination, diffing, caching, tests) depend on a repeatable order?
3. Null and missing values. Where do nulls, empty strings and absent keys sort? Is that the same everywhere this data is sorted, including in the database query that may feed it?
4. Text ordering. Is text compared by code point, case-insensitively, or with locale-aware collation? Will accented characters, mixed case or numeric strings ("item10" vs "item9") sort the way users expect?
5. Consistency with equality. If the code also uses equality or hashing (sets, maps, dedup), do equals/hash and compare agree on which items are "the same"?
6. Sort-then-assume. Does later code assume sorted input (binary search, merge, "first item is the max")? Is that assumption still true after this change?
For each finding give: the line, a concrete input that triggers the bug, the wrong result, and the smallest fix. Mark each as BLOCKING (wrong results or a crash) or NON-BLOCKING (surprising but harmless). If you find nothing in a category, skip it. Do not invent problems to fill the list.
---
When to use it: on any pull request that adds or changes a comparator, a sort key, an ORDER BY that must match in-memory sorting, or deduplication logic. These bugs pass most unit tests because test data rarely contains ties, nulls or non-ASCII text.
How to adapt it: fill in the language, because stability and collation defaults differ between runtimes. If the output feeds pagination, say so; the reviewer should then weigh tie-breaking as blocking. Remove items that cannot apply (for example item 4 for purely numeric keys).
Example input: a TypeScript diff that sorts products with `(a, b) => a.price - b.price` and pages through them with offset pagination.
What good output looks like: a BLOCKING finding that equal prices have no tie-breaker, so with offset pagination a product can appear on two pages or none; the trigger is two products at the same price straddling a page boundary; the fix is adding a unique secondary key such as `id`. It also checks whether `price` can be null or NaN in this data, and says nothing else if the rest is fine.
Sign in to upvote this prompt.