Skip to content
Open
Changes from 1 commit
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
27 changes: 25 additions & 2 deletions src/arrayMethodSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,31 @@
* Implement method Sort
*/
function applyCustomSort() {
[].__proto__.sort2 = function(compareFunction) {
// write code here
Array.prototype.sort2 = function (compareFunction) {

Check failure on line 7 in src/arrayMethodSort.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Array prototype is read only, properties should not be added
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The scaffold specifies [].__proto__.sort2 but Array.prototype.sort2 is used. While functionally equivalent, consider using the exact scaffold syntax for consistency.

const arr = this;

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

if (strA > strB) return 1;
if (strA < strB) return -1;
return 0;
};

const compare = compareFunction || defaultCompare;

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

return arr;
};
}

Expand Down
Loading