diff --git a/src/arrayMethodSort.js b/src/arrayMethodSort.js index 32363d0d..9e379ff3 100644 --- a/src/arrayMethodSort.js +++ b/src/arrayMethodSort.js @@ -1,11 +1,33 @@ 'use strict'; -/** - * Implement method Sort - */ +/* eslint-disable no-extend-native */ + function applyCustomSort() { - [].__proto__.sort2 = function(compareFunction) { - // write code here + Array.prototype.sort2 = function (compareFunction) { + const arr = this; + + const defaultCompare = (a, b) => { + const strA = String(a); + const strB = String(b); + + if (strA > strB) return 1; + if (strA < strB) return -1; + return 0; + }; + + const compare = compareFunction || defaultCompare; + + for (let i = 0; i < arr.length - 1; i++) { + 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; }; }