Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 616
user1876246 Avatar asked Jun 13 '14 19:06

user1876246


Video Answer


1 Answers

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

like image 94
XrXr Avatar answered Sep 19 '22 17:09

XrXr