1

I try to order a numeric array, but I don't get it. What is wrong?

An example: http://plnkr.co/edit/FheDOnr6ZmTJN0bFpgbB?p=preview

<select data-ng-model="current" data-ng-options="number as number for number in numbers | orderBy:'number'"></select>
1

1 Answer 1

6

You can use a function to order too:

<select 
 data-ng-model="current" 
 data-ng-options="number as number for number in numbers | orderBy:'valueOf()'">
</select>

Should work.

Why does it fail?

Cause you were saying to order by the property number which doesn't work cause they're just plain integers which doesn't have any property called number but they do have a valueOf method which does the trick.

You could also use just number without quotations (as suggested in a comment) so it will just pass the variable instead of a property.

Sign up to request clarification or add additional context in comments.

2 Comments

Its quite interesting thing you've shared Antonio. Will you please elaborate this line for me You could also use just number without quotations (as suggested in a comment) so it will just pass the variable instead of a property. ?
Sure @Vineet. When you use a string: orderBy:'number' angular tries to find the property number inside the value. In this case, they're integers so they can't have that property by default. However if you only pass number to orderBy with no quotes, you're passing the variable, that is, the raw integer so orderBy can work out the order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.