At the interview they asked the question: "How to sort an array without using the .sort () method using ES6, and the code should not take more than two lines".
Already an hour break over this head, I can not think of anything.
At the interview they asked the question: "How to sort an array without using the .sort () method using ES6, and the code should not take more than two lines".
Already an hour break over this head, I can not think of anything.
let a = [3, 2, 1, 1, 5, 4], c = []; for (; a.length;) { c.push(a.splice(a.indexOf(Math.min(...a)),1)[0])} console.log(c)
It turned out something like this:
let arr = [5,6,4,3,7]; console.log('input: ' + arr); let obj = arr.reduce((o, v, i) => { o[v] = v; return o; }, {}); arr = Object.keys(obj).map(num => parseInt(num)); console.log('output: ' + arr);
In one line cut)))
let arr = [5,6,4,3,7]; console.log('input: ' + arr); arr = Object.keys(arr.reduce((o, v, i) => { o[v] = v; return o; }, {})).map(num => parseInt(num)); console.log('output: ' + arr);
Maybe there are shoals. What surely others will happily prompt
UPD: because the above method returns a sorted array and at the same time non-repeating elements (that is, when entering 5,4,4,7,3
get 3,4,5,7
), here's another version .... at the request of the viewers, so that in the resulting array there were duplicates if there are
let arr = [5, 3, 6, 4, 4, 7]; console.log('input: ' + arr); let arrCopy = arr.slice(0, arr.length); let result = []; arrCopy.forEach((el, i) => { let index = min = 0; arr.reduce((a, b) => { min = (a <= b) ? a : b; return min; }) index = arr.indexOf(min); result.push(i == arrCopy.length - 1 ? arr[0] : arr[index]); arr.splice(index, 1); }); console.log('output: ' + result);
and last for today:
let arr = [5, 3, 6, 4, 4, 7]; console.log('input: ' + arr); let arrCopy = arr.slice(0, arr.length); let result = []; arrCopy.forEach(() => { let minElementIndex = 0; minElementIndex = arr.indexOf(Math.min.apply(Math, arr)); result.push(arr[minElementIndex]); arr.splice(minElementIndex, 1); }); console.log('output: ' + result);
[5,4,4,6]
: P - GrundyBased on the adjacent answer with reduce
, we run through the source array and look for where to insert the checked element into the final array. the search is performed using the findIndex function
!!! It works, even if there are elements with the same values in the array !!!
let arr = [5, 6, 4, 3, 7]; console.log('input: ' + arr); let sorted = arr.reduce((o, v) => { var index = o.findIndex(el => el > v); return ((index > -1) ? o.splice(index, 0, v) : o.push(v)), o; }, []); console.log('output: ' + sorted); let arrDup = [5, 6, 4, 5, 7, 3, 3, 7]; console.log('input: ' + arrDup); let sortedDup = arrDup.reduce((o, v) => { var index = o.findIndex(el => el > v); return ((index > -1) ? o.splice(index, 0, v) : o.push(v)), o; }, []); console.log('output: ' + sortedDup);
In most solutions, it all comes down to mapping the source array to the result, each time choosing the minimum or maximum element.
let arr = [5, 6, 4, 3, 4, 7]; console.log('input: ' + arr); let sorted = arr.map(function(el) { return this.splice(this.indexOf(Math.min(...this)), 1)[0]; }, arr.slice()); console.log('output: ' + sorted);
Such a perversion was born:
let arr = [3, 2, 1, 1, 5, 4]; let sorted = []; while (arr.length) sorted = sorted.concat(arr.splice(arr.indexOf(arr.reduce((prev, cur) => prev < cur ? prev : cur)), 1)); console.log(sorted);
arr.reduce
in this case, it is better to replace with Math.min.apply(Math,arr)
at least it will be clear what he does :-) - GrundySource: https://ru.stackoverflow.com/questions/566705/
All Articles