How would I sort arrays as follows:
[10, 7, 12, 3, 5, 6] --> [10, 12, 3, 5, 6, 7]
[12, 8, 5, 9, 6, 10] --> [12, 5, 6, 8, 9, 10]
My proposal is based on:
In this way the initial problem is divided in two sub problems that are more simple to work on.
function cSort(arr) {
var retval = arr.reduce(function (acc, val) {
acc[(val < arr[0]) ? 1 : 0].push(val);
return acc;
}, [[], []]);
return retval[0].sort((a, b) => a-b).concat(retval[1].sort((a, b) => a-b));
}
//
// test
//
console.log(cSort([10, 7, 12, 3, 5, 6]));
console.log(cSort([12, 8, 5, 9, 6, 10]));
console.log(cSort([12, 8, 5, 9, 12, 6, 10]));
console.log(cSort([12]));
console.log(cSort([]));