Nodejs Array Sort sortBy(index,sort)

Here you can find the source of sortBy(index,sort)

Method Source Code

Array.prototype.sortBy = function(index,sort){
    index = index || 0;//from  w w w.  j a  v  a2 s. c om
    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{
                    t = this[j];
                    this[j] = this[j-1];
                    this[j-1] = t;
                }
            }else{
                if(sort === "asc"){
                    t = this[j];
                    this[j] = this[j-1];
                    this[j-1] = t;
                }else{
                    //????
                }
            }
        }
    }
    return this;
};
console.log(["kitty","puppy","swan","penguin","giraffe","penguin","swan","dolphin"].unique());
console.log([ 'kitty', 'puppy', 'swan', 'penguin', 'dolphin', 'giraffe' ].sortBy(-2));
console.log(["kitty","puppy","swan","penguin","giraffe","penguin","swan","dolphin"].unique().sortBy(-2));

Related

  1. sortBy(()
    Array.prototype.sortBy = (function() { 
        function identity(v){return v;} 
        function ignoreCase(v){return typeof(v)==="string" ? v.toLowerCase() : v;} 
        function makeCompareFunction(f, opt){ 
            opt = typeof(opt)==="number" ? {direction:opt} : opt||{}; 
            if(typeof(f)!="function"){ 
                var prop = f; 
                f = function(v1){return !!v1[prop] ? v1[prop] : "";} 
            if(f.length === 1) { 
                var uf = f; 
                var preprocess = opt.ignoreCase?ignoreCase:identity; 
                f = function(v1,v2) {return preprocess(uf(v1)) < preprocess(uf(v2)) ? -1 : preprocess(uf(v1)) > preprocess(uf(v2)) ? 1 : 0;} 
            if(opt.direction === -1) return function(v1,v2){return -f(v1,v2)}; 
            return f; 
        return function(callbacks){ 
            var array = this; 
            function tb(func, opt) { 
                var x = typeof(this) == "function" ? this : false; 
                var y = makeCompareFunction(func, opt); 
                var f = x ? function(a, b) { 
                                return x(a,b) || y(a,b); 
                          : y; 
                f.thenBy = tb; 
                f.sort = () => array.sort(f); 
                return f; 
            return tb(callbacks); 
        }; 
    })();
    
  2. 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;
    ...
    
  3. sortBy(comparator)
    Array.prototype.sortBy = function(comparator) {
        return this.sort((a,b) => {
            return comparator(a) < comparator(b) ? -1 : 1
        })
    
  4. 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;
        });
    };
    ...
    
  5. 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
      })
    
  6. 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;
    ...
    
  7. 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;
      });
    };
    
  8. 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;
    ...
    
  9. 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;
        });
    };
    ...