title: Sort an array of objects in Angular2
body: <p>I have problems with sorting an array of object in Angular2.</p>
  
  <p>The object looks like:</p>
  
  <pre><code>[
    {
      "name": "t10",
      "ts": 1476778297100,
      "value": "32.339264",
      "xid": "DP_049908"
    },
    {
      "name": "t17",
      "ts": 1476778341100,
      "value": "true",
      "xid": "DP_693259"
    },
    {
      "name": "t16",
      "ts": 1476778341100,
      "value": "true",
      "xid": "DP_891890"
    }
  ]
  </code></pre>
  
  <p>And is being stored inside the <code>values</code> variable.</p>
  
  <p>All I want is to make the <code>*ngFor</code> loop sort it by the <code>name</code> property.</p>
  
  <pre><code>&lt;table *ngIf="values.length"&gt;
      &lt;tr *ngFor="let elem of values"&gt;
        &lt;td&gt;{{ elem.name }}&lt;/td&gt;
        &lt;td&gt;{{ elem.ts }}&lt;/td&gt;
        &lt;td&gt;{{ elem.value }}&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>Tried to do it with pipes, but failed miserably.
  Any help appreciated.</p>
  
  <p><strong>Plunker link</strong>: <a href="https://plnkr.co/edit/e9laTBnqJKb8VzhHEBmn?p=preview" rel="noreferrer">https://plnkr.co/edit/e9laTBnqJKb8VzhHEBmn?p=preview</a></p>
  
  <p><strong>Edit</strong></p>
  
  <p>My pipe:</p>
  
  <pre><code>import {Component, Inject, OnInit, Pipe, PipeTransform} from '@angular/core';
  
  @Component({
    selector: 'watchlist',
    templateUrl: './watchlist.component.html',
    styleUrls: ['./watchlist.component.css'],
    pipes: [ ArraySortPipe ]
  })
  @Pipe({
    name: "sort"
  })
  
  export class ArraySortPipe implements PipeTransform {
    transform(array: Array&lt;string&gt;, args: string): Array&lt;string&gt; {
      array.sort((a: any, b: any) =&gt; {
        if (a &lt; b) {
          return -1;
        } else if (a &gt; b) {
          return 1;
        } else {
          return 0;
        }
      });
      return array;
    }
  }
  </code></pre>
  
  <p>And just put the <code>pipe</code> name into html file:</p>
  
  <pre><code>&lt;tr *ngFor="let elem of values | sort"&gt;
  </code></pre>