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
35 changes: 33 additions & 2 deletions src/arrayMethodSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,39 @@
* Implement method Sort
*/
function applyCustomSort() {
[].__proto__.sort2 = function(compareFunction) {
// write code here
[].__proto__.sort2 = function (compareFunction) {
const array = this;
const length = array.length;

const compare =
compareFunction ||
function (a, b) {
const sA = String(a);
const sB = String(b);

if (sA > sB) {
return 1;
}

if (sA < sB) {
return -1;
}

return 0;
};

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

array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}

return array;
};
}

Expand Down