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.

  • one
    sort what? numbers? lines? objects of unlimited nesting? space? - Alexey Shimansky
  • Sorting Numbers - Andrey Krupskiy
  • 2
    Like a hack, you can write any of your implementations and simply arrange it in one line;) - Alexey Shimansky
  • I also laughed so much, but this is not what they wanted from me. - Andrey Krupskiy
  • ES2015 does not add any special sorting functions, that is, in any case, to sort an array, you need to apply sort to it, no matter the prototype or any external - Grundy

4 answers 4

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); 

    • sort me such an array: [5,4,4,6] : P - Grundy
    • @Grundy yes in that case there will be a cant ... but then two lines will definitely not work because you need a lot of operations ..... added one more code, but already more lines there - Alexey Shimansky
    • well, why map if you don’t return anything from it :-) - Grundy
    • @Grundy it was a deep night. corrected on forEach - Alexey Shimansky

    Based 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); 

    • one
      and why shout like that?)))) - Alexey Shimansky

    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 :-) - Grundy
    • Oh, that's logical. The head in the evening already badly cooks, about existence Math is banal forgot. I will not fix it anymore. :) - Yaant September
    • @Grundy Yaant is a good decision .... but it’s extremely difficult to give birth at an interview)) - Alexey Shimansky