Angular does not come with an orderBy filter out of the box, but if we decide what we need, we can easily create it. However, there are some caveats we need to know about speed and minimum. See below.
A simple pipe will look something like this.
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'sort' }) export class SortPipe implements PipeTransform { transform(ary: any, fn: Function = (a,b) => a > b ? 1 : -1): any { return ary.sort(fn) } }
This tube takes a sort function ( fn ) and gives it a default value that will sort the array of primitives in a reasonable way. We have the ability to override this sorting function if we want.
It does not accept the attribute name as a string, because attribute names are to be specified. They will change when we minimize our code, but minifiers are not smart enough to also minimize the value in the template string.
Sorting primitives (numbers and strings)
We could use this to sort an array of numbers or strings using the default comparator:
import { Component } from '@angular/core'; @Component({ selector: 'cat', template: ` {{numbers | sort}} {{strings | sort}} ` }) export class CatComponent numbers:Array<number> = [1,7,5,6] stringsArray<string> = ['cats', 'hats', 'caveats'] }
Sort an array of objects
If we want to sort an array of objects, we can give it a comparator function.
import { Component } from '@angular/core'; @Component({ selector: 'cat', template: ` {{cats | sort:byName}} ` }) export class CatComponent cats:Array<Cat> = [ {name: "Missy"}, {name: "Squoodles"}, {name: "Madame Pompadomme"} ] byName(a,b) { return a.name > b.name ? 1 : -1 } }
Cautions - Clean and Unclean Pipes
Angular 2 has the concept of clean and unclean pipes.
A clean pipe optimizes change detection using an object identifier. This means that the channel will only work if the input object changes its identifier, for example, if we add a new element to the array. He will not descend into objects. This means that if we change the nested attribute: this.cats[2].name = "Fluffy" , for example, the pipe will not be restarted. This helps Angular to be fast. Angular is clean by default.
An unclean pipe , on the other hand, will check the attributes of an object. This potentially makes it much slower. Since it cannot guarantee that the pipe function will work (it may be sorted differently depending on the time of day), an unclean channel will be triggered every time an asynchronous event occurs. This will significantly slow down your application if the array is large.
The pipe on top is clean. This means that it will only work when the objects in the array are immutable. If you change the cat, you must replace the entire cat object with a new one.
this.cats[2] = {name:"Tomy"}
We can change the above to an unclean channel by setting a clean attribute:
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'sort', pure: false }) export class SortPipe implements PipeTransform { transform(ary: any, fn: Function = (a,b) => a > b ? 1 : -1): any { return ary.sort(fn) } }
This pipe will sink into objects, but will be slower. Use with caution.