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
21 changes: 19 additions & 2 deletions src/arrayMethodSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,25 @@
* Implement method Sort
*/
function applyCustomSort() {
[].__proto__.sort2 = function(compareFunction) {
// write code here
[].__proto__.sort2 = function (compareFunction) {
const cmp =
compareFunction ??
function (s1, s2) {
return s1.toString() > s2.toString() ? 1 : -1;
Comment thread
AndreyKagaml marked this conversation as resolved.
Outdated
Comment thread
AndreyKagaml marked this conversation as resolved.
Outdated
};
Comment thread
AndreyKagaml marked this conversation as resolved.

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

this[j] = this[j + 1];
this[j + 1] = item;
}
}
}

return this;
};
}

Expand Down