sort keys with JSON.stringify()
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:
>> …from running JSON.stringify when declaring an object thus:
> 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
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.
More information about the es-discuss