This gives you the ability to arbitrarily order via user defined list (the first argument) and then to order by some comparison function if for instance a new key were added (length in this example).
Given the example:
{
"baz": ...,
"bar": ...,
"foo": ...,
"quux": ...,
}
Then listing all of the keys in your desired order: comp :: Text -> Text -> Ordering
comp = keyOrder ["foo","bar","baz","qux"] `mappend` comparing length
Note if you list all keys in the first argument of keyOrder it never goes to comparing length. There is probably a cleaner way to denote that this is the case but I can't be bothered to figure it out atm.You can use this function:
encodePretty' defConfig { confCompare = comp } YourType
And it will produce: {
"foo": ...,
"bar": ...,
"baz": ...,
"quux": ...,
}
Can you give a sample of the javascript in the order necessary? This is from NPM? They have to use some comparison function to figure out how to sort their keys I'd imagine, so you could just copy that with Haskell.