Angular2数字orderBy管道

时间:2017-09-19 17:41:01

标签: angular angular-pipe

我正在尝试使用自定义管道(我没有编写此管道)在Angular2中按数值对字符串数组进行排序。这是管道:

DocumentDeactivationType

这是对象。我需要能够通过" 标签"进行数字排序。

import { Pipe, PipeTransform } from "@angular/core";

@Pipe( { name: 'numericalSort' } )

export class NumericalSortPipe implements PipeTransform {
transform( array: Array<any>, orderField: string, orderType: boolean ): Array<string> {
    array.sort( ( a: any, b: any ) => {
        let ae = a[ orderField ];
        let be = b[ orderField ];
        if ( ae == undefined && be == undefined ) return 0;
        if ( ae == undefined && be != undefined ) return orderType ? 1 : -1;
        if ( ae != undefined && be == undefined ) return orderType ? -1 : 1;
        if ( ae == be ) return 0;
        return orderType ? (ae.toString().toLowerCase() > be.toString().toLowerCase() ? -1 : 1) : (be.toString().toLowerCase() > ae.toString().toLowerCase() ? -1 : 1);
    } );
    return array;
  }
}

以下是HTML示例:

{
  "id" : "12345678",
  "accountId" : "123456789",
  "label" : "906",
  "fullAddress" : {
    "zip" : "12345"
  }
}

我可以按默认的字母值排序。我怎样才能按数值排序?

3 个答案:

答案 0 :(得分:1)

我会在管道中添加一个新参数来指示字段的实际类型。因此,管道将能够将数组中的项字符串字段转换为实际类型:

export class NumericalSortPipe implements PipeTransform {
    transform(array: Array<any>, orderField: string, orderType: boolean, dataType: string): Array<string> {
        array.sort((a: any, b: any) => {
            let ae = a[orderField];
            let be = b[orderField];
            if (ae === undefined && be === undefined) return 0;
            if (ae === undefined && be !== undefined) return orderType ? 1 : -1;
            if (ae !== undefined && be === undefined) return orderType ? -1 : 1;
            if (ae === be) return 0;
            switch (dataType) {
                case "number":
                    ae = parseFloat(ae);
                    be = parseFloat(be);
                    break;
                case "string":
                    ae = ae.toString().toLowerCase();
                    be = be.toString().toLowerCase();
                    break;

                default:
                    break;
            }
            return orderType ? (ae > be ? -1 : 1) : (be > ae ? -1 : 1);
        });
        return array;
    }
}

用法:

<div *ngFor="let place of places | numericalSort: 'label':false:'number'">
    <h3>
        {{place.label}}
    </h3>
    {{place.id}}
</div>

请参阅plunk

答案 1 :(得分:0)

您可以使用lodash进行排序。

var myArray = [ 3, 4, 2, 9, 4, 2 ];

_.sortBy(myArray);
// → [ 2, 2, 3, 4, 4, 9 ]

_(myArray).sortBy().take(3).value();
// → [ 2, 2, 3 ]

答案 2 :(得分:0)

这是一个首先订购数字的管道,然后是字母:

@Pipe({name: "orderBy", pure: false})
export class OrderByPipe implements PipeTransform {
  transform(array: Array<any>) {
    return array.sort((a, b) => {
        const [stringA, numberA] = this.order(a);
        const [stringB, numberB] = this.order(b);
        const comparison = stringA.localeCompare(stringB);
        return comparison === 0 ? Number(numberA) - Number(numberB) : comparison;
  })
  }

   order (item) {
      const [, stringPart = '', numberPart = 0] = /(^[a-zA-Z]*)(\d*)$/.exec(item) || [];
  return [stringPart, numberPart];
  }
}

DEMO