Nodejs Array Sort sortByKeysortByKey(key, dsc)

Here you can find the source of sortByKeysortByKey(key, dsc)

Method Source Code

Array.prototype.sortByKey = function sortByKey(key, dsc) {
  return this.sort(function(a, b) {
      var x = a[key]; 
      var y = b[key];
      if (dsc ==='dsc') {
        return (x === undefined && y === undefined ? 0 : (x < y) || x === undefined ? 1 : ((x > y) || y === undefined ? -1 : 0));
      }/*from   w  w  w.j av a  2  s. c o  m*/
      return (x === undefined && y === undefined ? 0 : (x < y) || x === undefined ? -1 : ((x > y) || y === undefined ? 1 : 0)); 
  });
};

module.exports = {
  'sortByKey': Array.prototype.sortByKey
};

Related

  1. 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;
    ...
    
  2. 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;
        });
    };
    ...
    
  3. sortByFrequency()
    Array.prototype.sortByFrequency = function () {
      var freq = {};
      this.map(function(e, i, a){
        if(e in freq){
          freq[e]++;
        } else {
          freq[e] = 1;
      });
    ...
    
  4. sortByID()
    var library = [ 
           title:  'The Road Ahead',
           author: 'Bill Gates',
           libraryID: 1254
       },
           title: 'Walter Isaacson',
           author: 'Steve Jobs',
    ...
    
  5. 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',
    ...
    
  6. sortByNumber()
    Array.prototype.sortByNumber = function() {
     return this.sort(function(a, b) { return a - b; }); 
    
  7. sortDescending()
    function getLargestPalindrome() {
      products = fillMultiples().sortDescending();
      for (i = 0; i < products.length; i++) {
        if (products[i].toString().isPalindrome()) {
          return products[i];
    function fillMultiples() {
    ...
    
  8. sortNumber(a)
    Array.prototype.sortNumber = function(a) {
      if (a == true) {
        return this.sort(function(d, c) {
          return d - c
        }).reverse()
      } else {
        return this.sort(function(d, c) {
          return d - c
        })
    ...
    
  9. sortObject(field)
    Array.prototype.sortObject = function(field) {
      if (typeof this[0][field] === 'undefined') return this;
      var len = this.length,
        i,
        j,
        flag,
        next,
        temp;
      for (i = 1; i < len; i++) {
    ...