user1876246 user1876246 - 1 month ago 19
AngularJS Question

orderBy number angular js

I have an array of objects...

var userObjects =[
{userName: bob,
age: "25",
gender: "m"
},
{userName: bill,
age: "15",
gender: "m"
},
{userName: jen,
age: "45",
gender: "f"
},
]


Now I have an ng-repeat in my HTML to iterate over this array of objects. I want it to sort the age:

<tr ng-repeat="user in userObjects | orderBy:-age">


In my JS I am converting my ages to intergers....

data.forEach(function(user){
user.age= parseFloat(user.age);
}


Angular will not sort by age, I would expect to see...

bill, 15
bob, 25
jen, 45


But instead they are all over the place. What am I doing wrong?

Thanks!

Answer

Try wrapping -age in single quotes, like '-age'