Tito
Tito

Reputation: 852

AngularJS orderBy using lexicographical ordering even with numbers

I have a value function which is passed into my orderBy as:

function getValue(item){
    return [parseInt(item.approx_value_usd) || -1];
}

This definitely always returns a number array, but for some reason on the front-end AngularJS always orders my items by lexicographical order of the property 'approx_value_usd' e.g.

88 > 82 > 8 > 53 (wrong!)

I feel like I'm missing something but can't seem to get anywhere with this problem.

Upvotes: 2

Views: 489

Answers (1)

Pointy
Pointy

Reputation: 413737

The return value of the "order-by" function is examined using simple comparisons. Your code is returning an array, not just a number. When an array appears in a JavaScript > or < comparison, it'll be converted to a string. That's done by taking the string value of each element in the array and joining them.

Thus, even though you were putting numbers in the array, when Angular actually used the returned value it ends up being a string anyway. If you drop the [ ] it should work.

Upvotes: 2

Related Questions