0

I have the following Object which contains Objets in itself:

var myObj = {
    1:{ 
        id:1,
        name:'John',
        otherVal='LoremIpsum'
    },
    2:{
        id:2,
        name:'Bill',
        otherVal='LoremIpsum'
    },
    3:{
        id: 3,
        name:'Steve',
        otherVal='LoremIpsum'
    }};

I show the values of name inside these object in an select field in the following way:

<select name="someName" ng-model="someModel"
        ng-options="person.id as person.name for person in myObj"
    <option value=""></option>
</select>

How can I sort the <option>'s alphabetically by the value name?

mkoala
  • 893
  • 1
  • 7
  • 15

1 Answers1

2

It's quite simple.

Just update your code with the below one.

   <select name="someName" ng-model="someModel"
           ng-options="person.id as person.name for person in myObj | orderBy:'name'"
           <option value=""></option>
   </select>

OR

Go to: Sorting dropdown alphabetically in AngularJS

Surjeet Bhadauriya
  • 6,755
  • 3
  • 34
  • 52