You can't sort the properties in an object, because the order of the properties is not maintained. If create an object like that, then loop out the properties, you will see that the properties may not be returned in the same order that you put them in the object, and different browsers will return the properties in differend order.
Make the object an array, so that it can maintain the order of the values, and make the lookup array an object so that you can efficiently map a string to a numeric value:
var months = {
January: 1,
February: 2,
March: 3,
April: 4,
May: 5,
June: 6,
July: 7,
August: 8,
September: 9,
October: 10,
November: 11,
December: 12
};
var objects = [
{ name: 'April', value: 0 },
{ name: 'August', value: 4182 },
{ name: 'December', value: 0 },
{ name: 'February', value: 0 },
{ name: 'January', value: 1 },
{ name: 'July', value: 2 },
{ name: 'June', value: 0 },
{ name: 'March', value: 0 },
{ name: 'May', value: 0 },
{ name: 'November', value: 0 },
{ name: 'October', value: 0 },
{ name: 'September', value: 1518 }
];
Now you can sort the array using the object:
objects.sort(function(x,y) { return months[x.name] - months[y.name]; });
Demo: http://jsfiddle.net/7eKfn/
objects
is not valid,=
should be:
. – Chips_100 Sep 19 '13 at 9:15