{"record":{"id":"75d7121663432b55","repo":"trekhleb/javascript-algorithms","slug":"sort-method-must-be-implemented","errorCode":null,"errorMessage":"sort method must be implemented","messagePattern":"sort method must be implemented","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/algorithms/sorting/Sort.js","lineNumber":32,"sourceCode":"    this.comparator = new Comparator(this.callbacks.compareCallback);\n  }\n\n  /**\n   * @param {SorterCallbacks} originalCallbacks\n   * @returns {SorterCallbacks}\n   */\n  static initSortingCallbacks(originalCallbacks) {\n    const callbacks = originalCallbacks || {};\n    const stubCallback = () => {};\n\n    callbacks.compareCallback = callbacks.compareCallback || undefined;\n    callbacks.visitingCallback = callbacks.visitingCallback || stubCallback;\n\n    return callbacks;\n  }\n\n  sort() {\n    throw new Error('sort method must be implemented');\n  }\n}\n","sourceCodeStart":14,"sourceCodeEnd":35,"githubUrl":"https://github.com/trekhleb/javascript-algorithms/blob/85293e3e2b88f4d2ce330d956b139cf628aa1e82/src/algorithms/sorting/Sort.js#L14-L35","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nimport Sort from '../src/algorithms/sorting/Sort';\nnew Sort().sort();\n// throws: abstract method\n\n// after\nimport BubbleSort from '../src/algorithms/sorting/bubble-sort/BubbleSort';\nconst sorted = new BubbleSort().sort([3, 1, 2]);","handlingStrategy":"type-guard","validationCode":"import Sort from '../src/algorithms/sorting/Sort';\n\nif (!sorter || sorter.sort === Sort.prototype.sort) {\n  throw new TypeError('Use a concrete Sort subclass such as BubbleSort');\n}\nsorter.sort(input);","typeGuard":"import Sort from '../src/algorithms/sorting/Sort';\n\nconst hasConcreteSort = (s) =>\n  !!s && typeof s.sort === 'function' && s.sort !== Sort.prototype.sort;","tryCatchPattern":"try {\n  sorted = sorter.sort(arr);\n} catch (e) {\n  if (e.message === 'sort method must be implemented') {\n    throw new TypeError(sorter.constructor.name + ' does not implement sort()');\n  }\n  throw e;\n}","preventionTips":["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/*/"],"tags":["sorting","abstract-method","inheritance","oop"],"backgroundTag":"abstract-method-not-implemented","analyzedSha":"85293e3e2b88f4d2ce330d956b139cf628aa1e82","analyzedAt":"2026-08-24T05:59:10.417Z","schemaVersion":2},"datasetVersion":"2026-08-24T07:17:09.176Z"}