orderBy number angular js

12,974

Solution 1

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

Solution 2

Not necessary to convert strings to numbers in JS.

Make it in pure Angular with filter:

| orderBy:'-1*age'`

Solution 3

Following will convert the 'age' into numeric and sort in ascending order.

| orderBy : 'age/1'

Solution 4

Try this dude.....

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

Share:
12,974
user1876246
Author by

user1876246

Updated on June 09, 2022

Comments

  • user1876246
    user1876246 almost 2 years

    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!

  • ryanyuyu
    ryanyuyu about 8 years
    The OP is already including that code in their question. How does this help with anything?
  • Nagarjuna Yendluri
    Nagarjuna Yendluri over 6 years
    @ryanyuyu He has the 'age' in quotes, which is correct answer.