orderBy number angular js

user1876246 picture user1876246 · Jun 13, 2014 · Viewed 12.8k times · Source

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

XrXr picture XrXr · Jun 13, 2014

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