How to sort array in ng-options by key?

All we need is an easy explanation of the problem, so here it is.

There is such array:

month: Array[13]0: "M"1: "January"2: "February"3: "March"4: "April"5: "May"6: "June"7: "July"8: "August"9: "September"10: "October"11: "November"12: "December"

I do:

ng-options="key as value for (key, value) in data.month | orderBy:key"

But I get unsorted select list.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

In order to use tracking with filters use track by.


ng-options="key as value for (key, value) in data.month | orderBy:'key' track by key"


This orderBy will never work because you are having literal array. You need to convert this array to JSON like object which would structured like [{id: 0, value: "M"}, {id: 1, value: "January"},......]


ng-options=" as item.value for items in data.month | orderBy:'id'"

Demo Plunkr

Method 2

Your missing two '‘s. This part, orderBy: key should be orderBy:'key'

Try this:

ng-options="key as value for (key, value) in data.month | orderBy: 'key'"

Method 3

To resolve this issue need reformat ng-option like as: as day.value for day in data.month

And array data.month would be how as told user pankajparkar

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply