Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/arrayMethodSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,36 @@
* Implement method Sort
*/
function applyCustomSort() {
[].__proto__.sort2 = function(compareFunction) {
// write code here
[].__proto__.sort2 = function (compareFunction) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates [].__proto__.sort2, but the task requires implementing Array.prototype.sort using that predefined sort2. The file never assigns or implements Array.prototype.sort. Consider adding a line such as Array.prototype.sort = [].__proto__.sort2; (or similar) after defining sort2 so the built-in method is implemented.

if (
compareFunction !== undefined &&
typeof compareFunction !== 'function'
) {
throw new TypeError(
'The comparison function must be either a function or undefined',
);
}

for (let i = 0; i < this.length; i++) {
for (let o = 0; o < this.length - 1 - i; o++) {
Comment on lines +17 to +18

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a simple bubble sort here is allowed for the task, but it's O(n²) and will perform poorly on large arrays. Consider mentioning this limitation or replacing it with a more efficient algorithm (quicksort/mergesort) if performance matters.

Comment on lines +17 to +18

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation uses bubble sort (the nested loops starting here). That's acceptable for the task, but consider adding a short comment noting the O(n²) performance so future readers understand the limitation and why a more efficient algorithm (e.g., quicksort or mergesort) would be preferable in production.

let shouldSwap;
Comment on lines +17 to +19

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a short comment above these nested loops noting that this is a bubble sort (O(n²)) — it's acceptable for this task, but helps future readers understand the performance trade-off.


if (typeof compareFunction === 'function') {
shouldSwap = compareFunction(this[o], this[o + 1]) > 0;
} else {
shouldSwap = String(this[o]) > String(this[o + 1]);
}
Comment on lines +21 to +25

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a compareFunction is provided but is not a function, the native Array.prototype.sort throws a TypeError. Here the code silently falls back to string comparison. Consider validating compareFunction and throwing TypeError when it's not a function to match native behavior (or explicitly document the difference).


if (shouldSwap) {
[this[o], this[o + 1]] = [this[o + 1], this[o]];
}
}
}

return this;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You define [].__proto__.sort2 but never make Array.prototype.sort delegate to it. Add a delegation (for example, after the sort2 assignment) such as:

Array.prototype.sort = [].__proto__.sort2;

so calls to someArray.sort(...) use your custom implementation. Place that new line after the }; that ends the sort2 assignment (i.e., immediately after line 34).

};
/* eslint-disable-next-line no-extend-native */
Array.prototype.sort = [].__proto__.sort2;
}

module.exports = applyCustomSort;