sort keys with JSON.stringify()

Brian Kardell bkardell at gmail.com
Sun Sep 11 10:19:38 PDT 2011


If you have non-numeric keys they should be in insertion order.  Search the
archives, this is a much discussed topic.
On Sep 11, 2011 12:13 AM, "Felipe Gasper" <felipe at felipegasper.com> wrote:
> But even that is implementation-specific, right?
>
> Object key order can be completely random…at least, that’s always been
> my understanding.
>
> -F
>
> On 9/10/11 9:32 PM, Cryptic Swarm wrote:
>> run it through something like this:
>>
>> function sortKeys(obj) {
>> var ret = {}
>> Object.keys(obj).sort().forEach(function(name) {
>> ret[name] = obj[name]
>> })
>> return ret
>> }
>>
>> before passing it to JSON.stringify
>>
>>
>> On Sat, Sep 10, 2011 at 9:14 PM, Felipe Gasper <felipe at felipegasper.com
>> <mailto:felipe at felipegasper.com>> wrote:
>>
>> Hello,
>>
>> Is it possible to get sorted keys:
>>
>> {a:1,c:3}
>>
>> …from running JSON.stringify when declaring an object thus:
>>
>> {c:3,a:1}
>>
>> ??
>>
>> -FG
>> _________________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>
>> https://mail.mozilla.org/__listinfo/es-discuss
>> <https://mail.mozilla.org/listinfo/es-discuss>
>>
>>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110911/b2f9bb3e/attachment.html>


More information about the es-discuss mailing list