Javascript Array quickSort(prop)

Description

Javascript Array quickSort(prop)


Array.prototype.quickSort = function(prop) {
    if (this.length < 2)
        return this;
    // w  ww . j a  va2s  .c  o  m
    const property = (i) => prop(i) || Infinity;
    const pivot = this[Math.round(this.length / 2)];
    return this.filter(i => property(i) < property(pivot))
        .quickSort(prop)
        .concat(this.filter(i => property(i) === property(pivot)))
        .concat(this.filter(i => property(i) > property(pivot)).quickSort(prop));
}



PreviousNext

Related