I managed to make a function which seems to work.
But basically you're saying that even if the following code seems to
work in my browser, I shouldn't rely on it to work?

function sortProperties(obj){
  var arr=[],newO={};
  for (prop in obj){
    arr.push(prop);
  }
  arr=arr.sort();
  arr.forEach(function(prop){
    newO[prop]=obj[prop];
  });
  return newO;
}


On Fri, May 27, 2011 at 2:53 PM, Martin Honnen <[email protected]> wrote:
> Björn Söderqvist wrote:
>>
>> I have an object whose properties are not alphabetically sorted, like so:
>> {a:1, c:3, b:2}
>> I would now like to have a function which can return a similar object,
>> only with the properties sorted alphabetically:
>> {a:1, b:2,c:3}
>>
>> I'm stuck right now as I can't figure out how to do this correctly. Is
>> it necessary to go via an array, throw in a custom sorting method,
>> then throw the properties back into an object?
>
> The properties of an object are not ordered. And a for..in loop does not
> define any order in which properties are returned.
>
> --
>
>        Martin Honnen --- MVP Data Platform Development
>        http://msmvps.com/blogs/martin_honnen/
>
> --
> To view archived discussions from the original JSMentors Mailman list:
> http://www.mail-archive.com/[email protected]/
>
> To search via a non-Google archive, visit here:
> http://www.mail-archive.com/[email protected]/
>
> To unsubscribe from this group, send email to
> [email protected]
>

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to