Javascript Array sortBy(field, direction)

Description

Javascript Array sortBy(field, direction)

Array.prototype.sortBy = function (field, direction) {
  var asc = direction === 'asc'
  return this.sort(function (a, b) {
    if (a[field] < b[field]) return asc ? 1 : -1
    if (a[field] > b[field]) return asc ? -1 : 1
    return 0/*from   www  .  j  av  a 2s. c  o m*/
  })
}



PreviousNext

Related