JavaScript insert into Array at index

Problem

I know how to insert into array at index, when that index is already present. Say arr = [1, 2, 3, 4] and arr.splice(2, 0, 0) will result in [1, 2, 0, 3, 4]. However I have an empty array which is getting filled from an object which is not in any particular order. Actually array is used to sort the object:

var key  = parseInt(object[object_key].timestamp);
while (key && array_sorted[key]) { //Attempt at preventing overwriting elements with same timestamp
    key++;
}
key = key ? key : array_sorted.length + 1;
array_sorted.splice(key, 0, object[object_key].data);

Now this will build an array [object[0].timestamp, object[1].timestamp, object[2].timestamp, ...] instead of something like this [undefined, ..., undefined, object[4].timestamp, object[3].timestamp, undefined, object[0].timestamp, undefined, ..., undefined, object[1].timestamp]... How can I achieve sorting of an object of objects by timestamp (object's object's property) using this method? Is there a way to insert at index of an empty array?

Problem courtesy of: Xeos

Solution

To answer the actual question, Objects in Javascript do not guarantee any order.

To get the kind of order-based behaviour that you want out of your object of objects, you really need to convert it into an array of objects. You can then sort it with the sort method as per Anand's suggestion.

Or you could sort them as you go:

var objects = {
  1:{timestamp:"12345"},
  2:{timestamp:"34567"},
  3:{timestamp:"23456"}
}

var array = [];

for(var i in objects){

  var timestamp = parseInt(objects[i].timestamp);

  var added = false;

  for(var j in array){

    if(timestamp > parseInt(array[j].timestamp)){

      array.splice(j + 1, 0, objects[i]);

      added = true;

      break;

    }
  }

  if (!added) {

    array.unshift(objects[i]);

  }
}
Solution courtesy of: shennan

Discussion

I am not going specific but the given example below could help you to solve your problem.

lets say we have

var objArray=[{"Name":"Anand"},{"Name":"Jhon"},{"Name":"Paul"}];

now you can sort like

objArray.sort(function(a, b) {

         var textA = a.Name.toUpperCase();
         var textB = b.Name.toUpperCase():;

         return (textA < textB) ? 1 : (textA > textB) ? -1 : 0;
         //return (textA < textB) ? -1 : (textA > textB) ? 1 : 0; //for ascending.

  });

now you will find sorted array.

Discussion courtesy of: anand4tech

Not sure that I understood correctly your question. Actually there are several question's as I can see and I'll try to answer them:

Sort Object

You can't sort the object, but you can make an array containing object's properties keys or values sorted by some criteria.

var object = {
  a: {
    timestamp: 1385127590866
  },
  c: {
    timestamp: 1385127590872
  },
  b: {
    timestamp: 1385127590877
  }
},

keys = Object.keys(object), // Get object properties as array

sortedByKeys = keys.sort(function(key0, key1) { // ['a', 'b', 'c']
    if(key0 < key1) return -1;
    if(key1 > key0) return 1;
    return 0;
}),

sortedByTimestamp = keys.sort(function(key0, key1) { // ['a', 'c', 'b']
    return object[key1] - object[key0];
}),

sortedValues = [], i, j;
for(i=0, l=sortedByTimestamp.length; i<l; i++) { // [{timestamp:1385127590866}, {timestamp:1385127590872}, {timestamp:1385127590877}]
    sortedValues.push(object[sortedByTimestamp[i]]);
}

Create empty array an place item at specific index

var array = Array(Object.keys().length); // [undefined, undefined, undefined]
array[1] = object['a']; // [undefined, {timestamp:1385127590866}, undefined]

Hope it will help.

Discussion courtesy of: Vadim

This recipe can be found in it's original form on Stack Over Flow.