Pipes Angular 2: Sort Array of Objects
<p>There are similar questions but none of the answers have done the trick for me, so I would appreciate if you don't mark this as duplicate (unless you refer me to answer question that does solve the issue)</p>

<p>I have a Array of Objects <code>result:Array&lt;Object&gt;=[];</code> which is returned with these values:
<a href="https://i.stack.imgur.com/N12sP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/N12sP.png" alt="Array Object data"></a></p>

<p>On my template, I would like to sort the response based on count of 'likes'</p>

<pre><code>&lt;tr class *ngFor="let media of (result) | orderBy: 'data.likes.count'"&gt;
   &lt;td&gt;
     &lt;img src={{media.data.images.standard_resolution.url}} height="100" width="100"&gt;
     &lt;p&gt; {{media.data.likes.count}} &lt;/p&gt;
  &lt;/td&gt;
&lt;/tr&gt;
</code></pre>

<p>The sort pipe looks like this:</p>

<pre><code>import {Pipe, PipeTransform} from '@angular/core';

@Pipe({name: 'orderBy', pure: false})

export class SortPipe {

  transform(array: Array&lt;Object&gt;, args: string): Array&lt;Object&gt; {

    console.log("calling pipe");

    if (array == null) {
      return null;
    }

    array.sort((a: any, b: any) =&gt; {
        if (a[args] &lt; b[args] ){
        //a is the Object and args is the orderBy condition (data.likes.count in this case)
            return -1;
        }else if( a[args] &gt; b[args] ){
            return 1;
        }else{
            return 0;
        }
    });
    return array;
  }
}
</code></pre>

<p>When I run this code, it shows me an unordered response, rather then sorting it based on the likes. I should point that that when I do <code>console.log(a[args])</code> on the pipe I get undefined so I am probably not reading the value in the object field correctly. </p>