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
38 changes: 36 additions & 2 deletions src/arrayMethodSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,43 @@
* 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.

Optional: validate compareFunction type. If compareFunction is provided but is not a function, consider throwing a TypeError to better mirror native Array.prototype.sort behavior (not strictly required by tests but improves compatibility).

const cmp =
compareFunction ||
Comment on lines +8 to +9

Choose a reason for hiding this comment

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

As an optional improvement, you could add a check here to ensure that if compareFunction is provided, it is actually a function. If compareFunction is provided but is not a function, the native Array.prototype.sort throws a TypeError. Implementing this would make your custom sort's behavior even closer to the native one.

function (a, b) {
const strA = String(a);
const strB = String(b);

if (strA < strB) {
return -1;
}

if (strA > strB) {
return 1;
}

return 0;
};

for (let i = 0; i < this.length - 1; i++) {
for (let j = 0; j < this.length - i - 1; j++) {
if (cmp(this[j], this[j + 1]) > 0) {
const temp = this[j];

this[j] = this[j + 1];
this[j + 1] = temp;
}
}
}
Comment on lines +25 to +34

Choose a reason for hiding this comment

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

This bubble sort implementation works correctly. For future reference, if performance were a critical concern, more efficient sorting algorithms like QuickSort or MergeSort would be preferred, as bubble sort has a time complexity of O(n^2). However, for this task, it's perfectly acceptable.


return this;

Choose a reason for hiding this comment

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

Before returning from applyCustomSort, assign Array.prototype.sort to delegate to your sort2 implementation. For example:

Array.prototype.sort = function(compareFn) {
  return this.sort2(compareFn);
};

This is required by the task description (implement Array.prototype.sort using the predefined sort2).

};

Choose a reason for hiding this comment

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

Missing override of Array.prototype.sort to delegate to your sort2. Add something like Array.prototype.sort = function(compareFn) { return this.sort2(compareFn); }; immediately after the sort2 definition so array.sort() uses the custom implementation (this is required by the task description).

/* eslint-disable no-extend-native */
Array.prototype.sort = function (compareFn) {
return this.sort2(compareFn);
};
/* eslint-enable no-extend-native */
}

module.exports = applyCustomSort;