If I have this JS object literal:
var foo = {
Sussy: 4,
Billy: 5,
Jimmy: 2,
Sally: 1
};
How can I create a new, sorted object literal:
var bar = {
Sally: 1,
Jimmy: 2,
Sussy: 4,
Billy: 5
};
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Re: How to sort a JS Object?
Answer: You can’t. So instead, you need a more sophisticated data structure. You have many options:
I recommend solution 3 since it uses the JS mechanics to manage the ordering.
Examples:
Edited: updated code to fix typo. Thank you, @Martin Fido