How to sort an array in ng options by key?

There is such an 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 a list of unsorted selects.

+7
javascript angularjs select angularjs-filter angularjs-ng-options
source share
3 answers

To use tracking with filters, use track by .

Markup

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

Update

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

HTML

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

Demo plunkr

+10
source share

Two missing. ' This part of orderBy: key should be orderBy:'key'

Try the following:

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

To solve this problem, reformat the ng-option as:

 day.id as day.value for day in data.month 

And the data.month array will be as specified by user pankajparkar

+1
source share

All Articles