I have a very simple array of objects and I want to sort it using $filter('orderBy')
in javascript. Somehow it doesn't seem to work. jsFiddle
Here is the code
var app = angular.module('myApp', []);
app.controller('myController', function($scope, $filter){
var person = [{
name : "Saras"
},
{
name : "Arya"
}];
$filter('orderBy')(person, 'name');
console.log(person);
});
I don't understand why I can't get this to work? Help is appreciated. And the solution should be in JS not in HTML.
The "+" operator is used to order the data in ascending order and thde "-" operator is used to order the data in descending order.
Parameter Values String: If the array is an array of objects, you can sort the array by the value of one of the object properties. See the examples below. Function: You can create a function to organize the sorting. Array: Use an array if you need more than one object property to determine the sorting order.
display=this. addition. sort(); will sort the array in ascending order, to sort in descending order you need to pass a comparator function.
Adding + or - prefix on orderBy parameter will order by + (ascending) or -(desc);
example:
<li ng-repeat="x in customers | orderBy : '-city'">
{{x.name + ", " + x.city}}
</li>
more at : http://www.w3schools.com/angular/ng_filter_orderby.asp
Or if you want to console.log it then just add name as parameter in quotations :
$filter('orderBy')(person, 'name');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With