Nodejs Array Sort sortBy(p, type)

Here you can find the source of sortBy(p, type)

Method Source Code

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;
      });            //from   w w w  .j av a2s  .c  om
   } else {
      return this.slice(0).sort(function(a,b) {
         return (a[p] > b[p]) ? 1 : (a[p] < b[p]) ? -1 : 0;
      });   
   }   
};

Related

  1. 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;
        });
    };
    ...
    
  2. 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
      })
    
  3. 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{
    ...
    
  4. sortBy(keySelector)
    Array.prototype.sortBy = function (keySelector) {
        return this.slice().sort(function(a,b) {
            var aKey = keySelector(a),
                bKey = keySelector(b);
            if (aKey > bKey) {
                return 1;
            else if (bKey > aKey) {
                return -1;
    ...
    
  5. 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;
      });
    };
    
  6. 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;
        });
    };
    ...
    
  7. sortByFrequency()
    Array.prototype.sortByFrequency = function () {
      var freq = {};
      this.map(function(e, i, a){
        if(e in freq){
          freq[e]++;
        } else {
          freq[e] = 1;
      });
    ...
    
  8. sortByID()
    var library = [ 
           title:  'The Road Ahead',
           author: 'Bill Gates',
           libraryID: 1254
       },
           title: 'Walter Isaacson',
           author: 'Steve Jobs',
    ...
    
  9. sortByID(argument)
    'use strict';
    Array.prototype.sortByID = function(argument){
      this.sort(function(x,y){
        return x.libraryID > y.libraryID;
      });
    };
    var library = [ 
           title:  'The Road Ahead',
    ...