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
37 changes: 35 additions & 2 deletions src/arrayMethodSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,41 @@
* 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
const compare =
typeof compareFunction === 'function'
? compareFunction
: (a, b) => {
const left = String(a);

Check failure on line 12 in src/arrayMethodSort.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 10 spaces but found 12
const right = String(b);

Check failure on line 13 in src/arrayMethodSort.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 10 spaces but found 12

if (left > right) {

Check failure on line 15 in src/arrayMethodSort.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 10 spaces but found 12
return 1;

Check failure on line 16 in src/arrayMethodSort.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 12 spaces but found 14
}

Check failure on line 17 in src/arrayMethodSort.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 10 spaces but found 12

if (left < right) {

Check failure on line 19 in src/arrayMethodSort.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 10 spaces but found 12
return -1;

Check failure on line 20 in src/arrayMethodSort.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 12 spaces but found 14
}

Check failure on line 21 in src/arrayMethodSort.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 10 spaces but found 12

return 0;

Check failure on line 23 in src/arrayMethodSort.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 10 spaces but found 12
};

for (let i = 0; i < this.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 functionally correct. For future tasks or real-world applications, you might want to consider more efficient sorting algorithms like Merge Sort or Quick Sort, as bubble sort's performance (O(n^2)) can be slow for large arrays.

for (let j = 0; j < this.length - 1 - i; j++) {
const a = this[j];
const b = this[j + 1];

// Вызываем колбэк сравнения
// Если результат > 0, значит 'a' должно идти после 'b'
if (compare(a, b) > 0) {
// Меняем элементы местами (деструктуризация)
this[j] = b;
this[j + 1] = a;
}
}
}

return this;
};
}

Expand Down