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

// Se não houver compareFn, usamos o comportamento padrão
if (typeof compareFn !== 'function') {
compareFn = function (a, b) {
const strA = String(a);
const strB = String(b);

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

if (strA > strB) {
return 1;
}

return 0;
};
}

// Bubble Sort
for (let i = 0; i < length - 1; i++) {
for (let j = 0; j < length - 1 - i; j++) {
if (compareFn(arr[j], arr[j + 1]) > 0) {
// swap manual
const temp = arr[j];

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

return arr;
};
}

Expand Down