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
33 changes: 32 additions & 1 deletion 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) {
[].__proto__.sort2 = function (compareFunction) {
// write code here
const arr = this;

const compare =
compareFunction ||
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 < arr.length; i++) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This bubble sort implementation is correct and fulfills the task requirements. For future reference, when dealing with large arrays, you might want to explore more performant sorting algorithms like Quick Sort or Merge Sort, which have a better average time complexity (O(n log n)).

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