sort keys with JSON.stringify()
Felipe Gasper
felipe at felipegasper.com
Sun Sep 11 16:02:51 PDT 2011
On 9/11/11 5:05 PM, Douglas Crockford wrote:
> On 11:59 AM, Felipe Gasper wrote:
>> Is it possible to get sorted keys:
>>
>> {a:1,c:3}
>>
>> …from running JSON.stringify when declaring an object thus:
>>
>> {c:3,a:1}
> JSON objects are unordered. If order is important, then use an array.
That’s actually my point.
What I would like is a way to have two objects with identical data
serialize the same way--ideally (but, ok, not necessarily) with keys
sorted alphabetically.
The most immediately use for such a feature is to deep-compare two
objects, but there are probably other viable applications.
There apparently is no way to do this using browsers’ native serializers
since (as has been mentioned) they usually serialize according to
for..in order, which in turn depends on insertion order. I want a
serialization that is agnostic with regards to insertion order, that
only depends on what’s in the object here and now.
Does any of the JavaScript-based serializers offer such a feature?
-FG
More information about the es-discuss
mailing list