Nodejs Array Sort sortBy(keySelector)

Here you can find the source of sortBy(keySelector)

Method Source Code

Array.prototype.sortBy = function (keySelector) {
    return this.slice().sort(function(a,b) {
        var aKey = keySelector(a),
            bKey = keySelector(b);/*from w  w w  .  j a va  2s . c o  m*/
        
        if (aKey > bKey) {
            return 1;
        }
        else if (bKey > aKey) {
            return -1;
        }
        else {
            return 0;
        }
    });
};

Array.zip = function(left, right, combinerFunction) {
    var results = [];
    
    for(var counter = 0, len = Math.min(left.length, right.length); counter < len; counter++) {
        results.push(combinerFunction(left[counter], right[counter]));
    }
    
    return results;
};

Related

  1. sortBy()
    Array.prototype.sortBy = function() {
          function _sortByAttr(attr) {
              var sortOrder = 1;
              if (attr[0] == "-") {
                  sortOrder = -1;
                  attr = attr.substr(1);
              return function(a, b) {
                  var result = (a[attr] < b[attr]) ? -1 : (a[attr] > b[attr]) ? 1 : 0;
    ...
    
  2. sortBy(comparator)
    Array.prototype.sortBy = function(comparator) {
        return this.sort((a,b) => {
            return comparator(a) < comparator(b) ? -1 : 1
        })
    
  3. sortBy(f)
    Array.prototype.sortBy = function (f) {
        return this.sort(function (a, b) {
            if (a[f] < b[f])
                return -1;
            if (a[f] > b[f])
                return 1;
            return 0;
        });
    };
    ...
    
  4. 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
      })
    
  5. sortBy(index,sort)
    Array.prototype.sortBy = function(index,sort){
        index = index || 0;
        sort = sort || "asc";
        var i, j, t, len = this.length;
        for(i = 0 ; i < len ; i ++){
            for(j = 1 ; j < len ; j++){
                if(this[j].charCodeAt(index >= 0 ? index : this[j].length+index) > this[j-1].charCodeAt(index >= 0 ? index : this[j-1].length+index)){
                    if(sort === "asc"){
                    }else{
    ...
    
  6. sortBy(p)
    Array.prototype.sortBy = function(p) {
      return this.slice(0).sort(function(a,b) {
        return (a[p] > b[p]) ? 1 : (a[p] < b[p]) ? -1 : 0;
      });
    };
    
  7. sortBy(p, type)
    Array.prototype.sortBy = function(p, type) {
      if(type == undefined) type = "asc";
      if(type == "desc"){
        return this.slice(0).sort(function(a,b) {
          return (a[p] < b[p]) ? 1 : (a[p] > b[p]) ? -1 : 0;
        });        
      } else {
        return this.slice(0).sort(function(a,b) {
          return (a[p] > b[p]) ? 1 : (a[p] < b[p]) ? -1 : 0;
    ...
    
  8. sortByDesc(f)
    Array.prototype.sortByDesc = function (f) {
        return this.sort(function (a, b) {
            if (a[f] < b[f])
                return 1;
            if (a[f] > b[f])
                return -1;
            return 0;
        });
    };
    ...
    
  9. sortByFrequency()
    Array.prototype.sortByFrequency = function () {
      var freq = {};
      this.map(function(e, i, a){
        if(e in freq){
          freq[e]++;
        } else {
          freq[e] = 1;
      });
    ...