Nodejs Array Sort sortDescending()

Here you can find the source of sortDescending()

Method Source Code

function getLargestPalindrome() {
  products = fillMultiples().sortDescending();
  for (i = 0; i < products.length; i++) {
    if (products[i].toString().isPalindrome()) {
      return products[i];
    }//from  www . j a v  a  2s  .c o m
  }
}

function fillMultiples() {
  var products = []
  for (i = 100; i < 1000; i++ ) {
    for (j = 100; j < 1000; j++ ) {
      products.push(i*j)
    }
  }
  return products
}

Array.prototype.sortDescending = function() {
  return this.sort(function(a,b) { return b - a })
}

String.prototype.isPalindrome = function() {
  return this == this.reverse() ? true : false
}

String.prototype.reverse = function() {
    return this.split('').reverse().join('');
}

console.log(getLargestPalindrome());

Related

  1. sortByFrequency()
    Array.prototype.sortByFrequency = function () {
      var freq = {};
      this.map(function(e, i, a){
        if(e in freq){
          freq[e]++;
        } else {
          freq[e] = 1;
      });
    ...
    
  2. sortByID()
    var library = [ 
           title:  'The Road Ahead',
           author: 'Bill Gates',
           libraryID: 1254
       },
           title: 'Walter Isaacson',
           author: 'Steve Jobs',
    ...
    
  3. 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',
    ...
    
  4. sortByKeysortByKey(key, dsc)
    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));
          return (x === undefined && y === undefined ? 0 : (x < y) || x === undefined ? -1 : ((x > y) || y === undefined ? 1 : 0)); 
      });
    ...
    
  5. sortByNumber()
    Array.prototype.sortByNumber = function() {
     return this.sort(function(a, b) { return a - b; }); 
    
  6. 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
        })
    ...
    
  7. 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++) {
    ...
    
  8. sort_text(direction, index)
    Array.prototype.sort_text = function(direction, index) {
        direction = direction === undefined ? 'asc' : direction;
        direction = direction === 'asc' ? 1 : -1;
        this.sort(function(a, b) {
            if (a[index] < b[index]) return -direction;
            if (a[index] > b[index]) return direction;
            return 0;
        })
    };
    ...
    
  9. sort()
    Array.prototype.sort = function(){
      var a = this;
      for(var i = 0;i<a.length-1;i++){
        var t = i;
        for(var j = i+1;j<a.length;j++)
          if(a[t]>a[j])
            t = j;
        if(t!=i)
          this.swap(i,t);
    ...