zoukankan      html  css  js  c++  java
  • 【angular5项目积累总结】自定义管道 OrderBy

    import { Injectable, Pipe } from '@angular/core';  
      
      
    @Pipe({  
      name: 'orderBy'  
    })  
    @Injectable()  
    export class OrderBy {  
      /* 
        Takes a value and makes it lowercase. 
       */  
    static _orderByComparator(a:any, b:any):number{  
          
        if((isNaN(parseFloat(a)) || !isFinite(a)) || (isNaN(parseFloat(b)) || !isFinite(b))){  
          //Isn't a number so lowercase the string to properly compare  
          if(a.toLowerCase() < b.toLowerCase()) return -1;  
          if(a.toLowerCase() > b.toLowerCase()) return 1;  
        }  
        else{  
          //Parse strings as numbers to compare properly  
          if(parseFloat(a) < parseFloat(b)) return -1;  
          if(parseFloat(a) > parseFloat(b)) return 1;  
        }  
          
        return 0; //equal each other  
      }  
      
      transform(input:any, [config = '+']): any{  
        if(!Array.isArray(input)) return input;  
      
        if(!Array.isArray(config) || (Array.isArray(config) && config.length == 1)){  
          var propertyToCheck:string = !Array.isArray(config) ? config : config[0];  
          var desc = propertyToCheck.substr(0, 1) == '-';  
                  
           //Basic array  
           if(!propertyToCheck || propertyToCheck == '-' || propertyToCheck == '+'){  
             return !desc ? input.sort() : input.sort().reverse();  
           }  
           else {  
             var property:string = propertyToCheck.substr(0, 1) == '+' || propertyToCheck.substr(0, 1) == '-'  
               ? propertyToCheck.substr(1)  
               : propertyToCheck;  
      
              return input.sort(function(a:any,b:any){  
                return !desc ?  OrderBy._orderByComparator(a[property], b[property])  
                     : -OrderBy._orderByComparator(a[property], b[property]);  
              });  
            }  
          }  
          else {  
            //Loop over property of the array in order and sort  
            return input.sort(function(a:any,b:any){  
              for(var i:number = 0; i < config.length; i++){  
                var desc = config[i].substr(0, 1) == '-';  
                var property = config[i].substr(0, 1) == '+' || config[i].substr(0, 1) == '-'  
                  ? config[i].substr(1)  
                  : config[i];  
      
                var comparison = !desc ?  
                     OrderBy._orderByComparator(a[property], b[property])  
                    : -OrderBy._orderByComparator(a[property], b[property]);  
                          
                //Don't return 0 yet in case of needing to sort by next property  
                if(comparison != 0) return comparison;  
              }  
      
            return 0; //equal each other  
          });  
        }  
      }  
    }  

     自定义排序 公共方法

    import { Injectable } from '@angular/core';
    import { Subject } from 'rxjs/Subject';
    @Injectable()
    export class CommonService {
        private notify = new Subject<any>();
        /**
         * Observable string streams
         */
        notifyObservable$ = this.notify.asObservable();
    
        constructor() { }
    
        public notifyOther(data: any) {
            if (data) {
                this.notify.next(data);
            }
        }
        public orderBy(propertyName: string, direction: number = 0): any {
            let comparer = (pre, next) => {
                let _pre = pre[propertyName];
                let _next = next[propertyName];
                if (_pre == 0 && !_next) return direction ? -1 : 1;
                if (_next == 0 && !_pre) return direction ? 1 : -1;
                if ((!_pre && !_next) || _pre == _next) return 0;
                if (!_pre && _next) return direction ? 1 : -1;
                if (_pre && !_next) return direction ? -1 : 1;
                   
                if (typeof _pre == 'string' && typeof _next == 'string') {
                    return direction == 0 ?
                        _pre && _pre.localeCompare(_next) : _next && _next.localeCompare(_pre);
                } else {
                    _pre = parseFloat(_pre);
                    _next = parseFloat(_next);
                    return direction == 0 ? _pre - _next : _next - _pre;
                }
            }
            return comparer;
        }
    }
    
    ---- 使用 ----
    this.lstGroups.sort(this.comService.orderBy(colName, direction));
  • 相关阅读:
    poj 2411 Mondriaan's Dream 骨牌铺放 状压dp
    zoj 3471 Most Powerful (有向图)最大生成树 状压dp
    poj 2280 Islands and Bridges 哈密尔顿路 状压dp
    hdu 3001 Travelling 经过所有点(最多两次)的最短路径 三进制状压dp
    poj 3311 Hie with the Pie 经过所有点(可重)的最短路径 floyd + 状压dp
    poj 1185 炮兵阵地 状压dp
    poj 3254 Corn Fields 状压dp入门
    loj 6278 6279 数列分块入门 2 3
    VIM记事——大小写转换
    DKIM支持样本上传做检测的网站
  • 原文地址:https://www.cnblogs.com/sybboy/p/8779922.html
Copyright © 2011-2022 走看看