javascript - Why does this lose sort-order? -


for purposes of our frontend app, need ingest array of objects (say 20), convert array key/value object.

data = [     {name: 'first', uid: 789, start: '2016-01-20 08:00:00'},     {name: 'second', uid: 492, start: '2016-01-20 15:00:00'},     {name: 'third', uid: 324, start: '2016-01-20 10:00:00'},     {name: 'fourth', uid: 923, start: '2016-01-20 14:30:00'},     // ... ]; 

i run sort on start

data.sort(function (a, b) {     var astart = new date(a.start),         bstart = new date(b.start);      if (astart < bstart) return -1;     if (astart > bstart) return 1;     return 0; }); 

then, purpose of quick-access data based on uid, loop through sorted array convert k/v object:

var stored = {}; (var = 0; < data.length; i++) {     stored[data[i].uid] = data[i]; } 

this allows me stored[uid] instead of having loop through data every time need index of given object.

issue

in looping through , creating stored object, seem losing sort order.

after sorting:

2016-01-20 08:00:00 // 789 2016-01-20 10:00:00 // 324 2016-01-20 14:30:00 // 923 2016-01-20 15:00:00 // 492 

after converting object

object.keys(sorted).map(function (id, index) {     console.log(sorted[id].start) }); 

yields:

2016-01-20 08:00:00 // 789 2016-01-20 14:30:00 // 923 2016-01-20 15:00:00 // 492 2016-01-20 10:00:00 // 324 

as can see, 10am event (324) @ end of list , not sure why happens.

as thefourtheye says, while engines preserve insertion order in objects, not requirement. there 2 ways keep order of properties:

  • keep array of sorted keys; iterate on array access object properties in order.

var data = [    {name: 'first', uid: 789, start: '2016-01-20 08:00:00'},    {name: 'second', uid: 492, start: '2016-01-20 15:00:00'},    {name: 'third', uid: 324, start: '2016-01-20 10:00:00'},    {name: 'fourth', uid: 923, start: '2016-01-20 14:30:00'}  ];    var stored = {};  (var = 0; < data.length; i++) {    stored[data[i].uid] = data[i];  }    var keys = object.keys(stored);  keys.sort(function(a, b) {    var astart = new date(stored[a].start),    var bstart = new date(stored[b].start);      if (astart < bstart) return -1;    if (astart > bstart) return 1;    return 0;  });    keys.foreach(function(key) {    console.log(stored[key].start);  });
<!-- results pane console output; see http://meta.stackexchange.com/a/242491 -->  <script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

  • use es6 map, guaranteed keep insertion order.

var data = [    {name: 'first', uid: 789, start: '2016-01-20 08:00:00'},    {name: 'second', uid: 492, start: '2016-01-20 15:00:00'},    {name: 'third', uid: 324, start: '2016-01-20 10:00:00'},    {name: 'fourth', uid: 923, start: '2016-01-20 14:30:00'}  ];    data.sort(function(a, b) {    var astart = new date(a.start),      bstart = new date(b.start);      if (astart < bstart) return -1;    if (astart > bstart) return 1;    return 0;  });    var stored = new map();  (var = 0; < data.length; i++) {    stored.set(data[i].uid, data[i]);  }    stored.foreach(function(value) {    console.log(value.start);  });
<!-- results pane console output; see http://meta.stackexchange.com/a/242491 -->  <script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -