sorting: Comparable vs Comparator from first principles, comparing/thenComparing/nullsFirst chaining, records as sort keys (and the record/equals compareTo trap), and a real reproduction of the int-subtraction-overflow comparator bug

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01YXCrLgRKFgCh9RHKW8xaqJ
This commit is contained in:
Claude
2026-10-01 16:19:14 +00:00
parent 8eb601aa0e
commit dfd3a830cd
25 changed files with 883 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
#!/usr/bin/env bash
# Regenerates every file in output/ from scratch: compile, run every demo, run the test suite.
set -euo pipefail
cd "$(dirname "$0")/.."
mvn -q -B compile
CP="target/classes"
java -cp "$CP" com.ankurm.sorting.OldSchoolComparatorDemo 2>&1 \
| grep -vE 'JAVA_TOOL_OPTIONS|^WARNING' > output/01-old-school-comparator.txt
java -cp "$CP" com.ankurm.sorting.ComparableNaturalOrderDemo 2>&1 \
| grep -vE 'JAVA_TOOL_OPTIONS|^WARNING' > output/02-comparable-natural-order.txt
java -cp "$CP" com.ankurm.sorting.ComparatorFactoryMethodsDemo 2>&1 \
| grep -vE 'JAVA_TOOL_OPTIONS|^WARNING' > output/03-comparator-factory-methods.txt
java -cp "$CP" com.ankurm.sorting.TreeSetOrderingDemo 2>&1 \
| grep -vE 'JAVA_TOOL_OPTIONS|^WARNING' > output/04-treeset-ordering.txt
java -cp "$CP" com.ankurm.sorting.RecordOrderingDemo 2>&1 \
| grep -vE 'JAVA_TOOL_OPTIONS|^WARNING' > output/05-record-ordering.txt
java -cp "$CP" com.ankurm.sorting.IntegerOverflowBugDemo 2>&1 \
| grep -vE 'JAVA_TOOL_OPTIONS|^WARNING' > output/06-integer-overflow-bug.txt
mvn -q -B test
cp target/surefire-reports/com.ankurm.sorting.SortingTest.txt output/07-tests.txt
echo "Regenerated output/01-07."
+6
View File
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
# Run one of the demo classes ad hoc, e.g.: ./scripts/run.sh IntegerOverflowBugDemo
set -euo pipefail
cd "$(dirname "$0")/.."
mvn -q -B compile
java -cp target/classes "com.ankurm.sorting.$1"