If an element is out of order, remove it from the array.

An example in everyone’s (least) favorite language:

const americanSort = (list, compare) => list.reduce((acc, cur) => { const lastElement = acc.length > 0 ? acc[acc.length - 1] : null; return lastElement ? compare(lastElement, cur) < 1 ? [...acc, cur] : acc : [cur]; }, []);

console.log(americanSort([5, 2, 6, 5, 20, 10, 40], (a, b) => a - b)) outputs Array(4) [ 5, 6, 20, 40 ]

  • @CannotSleep420OP
    link
    11 year ago

    It makes me feel cool and I’m already used to doing it, so doing it the function way becomes slower from being so used to doing it the other way.