2

Apparently ng-options is sorting my array lexographically . How can I prevent this default behavior?

This is the months object

"months": {
    "01": "January",
    "02": "February",
    "03": "March",
    "04": "April",
    "05": "May",
    "06": "June",
    "07": "July",
    "08": "August",
    "09": "September",
    "10": "October",
    "11": "November",
    "12": "December"
}

And this is the html generated by ng-options.

 <select ng-model="avtripExpMonth" name="avtripExpMonth" ng-options="k as v for (k,v) in months">
    <option value="?" selected="selected"></option>
    <option value="string:10" label="October">October</option>
    <option value="string:11" label="November">November</option>
    <option value="string:12" label="December">December</option>
    <option value="string:01" label="January">January</option>
    <option value="string:02" label="February">February</option>
    <option value="string:03" label="March">March</option>
    <option value="string:04" label="April">April</option>
    <option value="string:05" label="May">May</option>
    <option value="string:06" label="June">June</option>
    <option value="string:07" label="July">July</option>
    <option value="string:08" label="August">August</option>
    <option value="string:09" label="September">September</option>
 </select>

note: This is not a duplicate of How to use natural sorting in ng-options because this question is asking how to prevent the apparent default sorting behavior, not how to cause sorting to occur.

Community
  • 1
  • 1
steampowered
  • 11,809
  • 12
  • 78
  • 98
  • possible duplicate of [How to use natural sorting in ng-options?](http://stackoverflow.com/questions/18166802/how-to-use-natural-sorting-in-ng-options) – ooozguuur Jul 28 '15 at 14:18
  • Using the same code, i get the expected behavior : http://plnkr.co/edit/teuU4TmDV0PC3NrC153l?p=preview – Okazari Jul 28 '15 at 14:24
  • I'm not able to reproduce this in a jsFiddle: https://jsfiddle.net/6hak6uc8/. What version of Angular are you using? – Robert Nubel Jul 28 '15 at 14:24
  • 2
    javascript objects have no order, use arrays. What you have shown is not an array – charlietfl Jul 28 '15 at 14:25
  • 1
    @Okazari and Robert Nubel apparently Angular is sorting the array somewhere else **prior** to the object arriving at the controller. I will uncover this and either post my own answer or delete this question after I figure it out. Thanks for the plunker/jsfiddles. – steampowered Jul 28 '15 at 14:45

1 Answers1

1

As one of the commenters pointed out, javascript objects do not have an order, so the language itself doesn't support my expectation to iterate over the objects properties in a particular order.

Most browsers do iterate over properties in a json object in the order the object was defined or in the order which new properties were created. However, not always. Google chrome apparently preserves order when property keys case to non-numeric integers, but not for keys which cast to integers. Here is a google groups discussion on this topic: https://code.google.com/p/v8/issues/detail?id=164

Also worth noting, ECMAScript 6 specifies a new data structure called Map which has the capabilities of an object while preserving order.

steampowered
  • 11,809
  • 12
  • 78
  • 98