trekhleb/javascript-algorithms · error · Error
sort method must be implemented
Error message
sort method must be implemented
What it means
Sort (src/algorithms/sorting/Sort.js) is an abstract base class: its constructor wires up a Comparator and visiting callbacks, but sort() exists only to be overridden and unconditionally throws. Concrete subclasses (BubbleSort, SelectionSort, MergeSort, QuickSort and the other files under src/algorithms/sorting/*/) extend Sort and implement sort(). You hit this error by instantiating Sort itself or by writing a subclass that forgets to override sort() - the repo's own doForbiddenSort test exercises exactly this path.
Source
Thrown at src/algorithms/sorting/Sort.js:32
this.comparator = new Comparator(this.callbacks.compareCallback);
}
/**
* @param {SorterCallbacks} originalCallbacks
* @returns {SorterCallbacks}
*/
static initSortingCallbacks(originalCallbacks) {
const callbacks = originalCallbacks || {};
const stubCallback = () => {};
callbacks.compareCallback = callbacks.compareCallback || undefined;
callbacks.visitingCallback = callbacks.visitingCallback || stubCallback;
return callbacks;
}
sort() {
throw new Error('sort method must be implemented');
}
}
View on GitHub (pinned to 85293e3e2b)
Solutions
- Import and instantiate a concrete subclass instead, e.g. BubbleSort from src/algorithms/sorting/bubble-sort/BubbleSort
- If you wrote your own subclass, implement sort() (typically using this.comparator and this.callbacks.visitingCallback)
- If the base class is used only for typing or shared callbacks, never call sort() on it directly
Example fix
// before import Sort from '../src/algorithms/sorting/Sort'; new Sort().sort(); // throws: abstract method // after import BubbleSort from '../src/algorithms/sorting/bubble-sort/BubbleSort'; const sorted = new BubbleSort().sort([3, 1, 2]);
Defensive patterns
Strategy: type-guard
Validate before calling
import Sort from '../src/algorithms/sorting/Sort';
if (!sorter || sorter.sort === Sort.prototype.sort) {
throw new TypeError('Use a concrete Sort subclass such as BubbleSort');
}
sorter.sort(input); Type guard
import Sort from '../src/algorithms/sorting/Sort'; const hasConcreteSort = (s) => !!s && typeof s.sort === 'function' && s.sort !== Sort.prototype.sort;
Try / catch
try {
sorted = sorter.sort(arr);
} catch (e) {
if (e.message === 'sort method must be implemented') {
throw new TypeError(sorter.constructor.name + ' does not implement sort()');
}
throw e;
} Prevention
- Treat Sort as an interface: import concrete subclasses only
- Start custom subclasses from a working sibling (BubbleSort) so sort() gets implemented
- Code-review rule: no imports of Sort.js outside src/algorithms/sorting/*/
When it happens
Trigger: new Sort().sort(); importing Sort instead of a concrete algorithm via IDE autocomplete; declaring class CustomSort extends Sort without a sort() method and calling it; a refactor renaming sort() in the subclass and silently losing the override.
Common situations: Auto-import picking the base class over a similarly named subclass, copy-pasting a subclass skeleton and deleting the method body, or wrapper code typed against the base class that accidentally receives a bare Sort instance.
Related errors
AI-assisted analysis of trekhleb/javascript-algorithms@85293e3e2b (2026-08-24).
Data as JSON: /api/errors/75d7121663432b55.
Report an issue: GitHub.