Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible to specify order of properties? #6

Open
ThomasDotCodes opened this issue Sep 12, 2016 · 3 comments
Open

Possible to specify order of properties? #6

ThomasDotCodes opened this issue Sep 12, 2016 · 3 comments

Comments

@ThomasDotCodes
Copy link

Let's say I have the following JSON object:

{
"name" : "John",
"age" : 55,
"sex" : "M"
}

using sortify, of course it will sort them by alphabetical order. However, what if I always wanted the JSON to be exported in the format as above? Is there a way to pass in similar to a whitelist--but have the whitelist retain the order as I enter it?

For example to replicate structure above, the call would be:

JSON.sortify(data, ['name', 'age', 'sex']);

@mk-pmb
Copy link

mk-pmb commented Jan 5, 2017

We'd have to use another API though, because an array would imply that only those properties are kept.

@ThomasR
Copy link
Owner

ThomasR commented Jan 13, 2017

Right.
JSON.sortify was built to provide maximal compatibility with JSON.stringify. Providing a custom order as suggested by @producerism would break that.

We could introduce a fourth parameter though: JSON.sortify(obj, replacer, space, customOrderHere)

In order to retain JSON.sortify.length === 3, we must access this using arguments[3] rather than a named parameter.

@ThomasR
Copy link
Owner

ThomasR commented Jan 13, 2017

I implemented the abovementioned in branch/customSorting. Please check if that fits your needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants