Define different sort order based on nested objects
See original GitHub issueIn the current implementation, you can’t define different sort order based on nested objects
imagine I want a
to be before b
in root element and b
to be before a
under x
element, as far as I can see, is not possible.
I’ve already created a solution for this on my local branch and would love to push it.
There are 3 options here that I see:
- Adjust the arguments to be nested - sort key will be called with the key with nested name
test('Dumper should provide nested keys', function () {
var nested = { a: 1, b: 2, x: { a: 2, b: 1 } };
var expected = 'a: 1\nb: 2\nx:\n b: 1\n a: 2\n';
var order = [ 'a', 'b', 'x', 'x.b', 'x.a' ];
assert.deepEqual(yaml.safeDump(nested, {
sortKeys: function (a, b) {
return order.indexOf(a) - order.indexOf(b);
}
}), expected);
- Pro: Nice clean and friendly API
- Con: Break backward compatibility
- Ad a third argument to sortKey - namespace (array)
test('Dumper should provide nested keys', function () {
var nested = { a: 1, b: 2, x: { a: 2, b: 1 } };
var expected = 'a: 1\nb: 2\nx:\n b: 1\n a: 2\n';
var rootOrder = [ 'a', 'b', 'x' ];
var xOrder = [ 'b', 'a' ];
assert.deepEqual(yaml.safeDump(nested, {
sortKeys: function (a, b, namespace) {
if (namespace && namespace.length === 1 && namespace[0] === 'x') { // an ugly way to check if namespace === ['x']
return xOrder.indexOf(a) - xOrder.indexOf(b);
}
return rootOrder.indexOf(a) - rootOrder.indexOf(b);
}
}), expected);
- Pro: backward compatible, doesn’t add ‘.’ as a special character
- Con: a sort function with a third parameter - yack!
- Do not fix ( I’ll fork and use my own implementation)
I think you should go with option 2, it has an API flaw but it
_Originally posted by @ekeren in https://github.com/nodeca/js-yaml/pull/188/comment#issuecomment-434783584_
Issue Analytics
- State:
- Created 5 years ago
- Comments:11 (5 by maintainers)
Top Results From Across the Web
How to sort a JavaScript array of objects by nested object ...
It works with arrays like this: sort('property', [ {property:'1'}, {property:'3'}, {property:'2'}, {property:'4'}, ]); But I want to be able to ...
Read more >Sort Data in a Visualization - Tableau Help
Data Source Order sorts based on how the data is sorted in the data source. Generally for relational data sources, this tends to...
Read more >Nested field type | Elasticsearch Guide [8.5] | Elastic
The nested type is a specialised version of the object data type that allows arrays of objects to be indexed in a way...
Read more >Sort
Defines on which nested object to sort. The actual sort field must be a direct field inside this nested object. When sorting by...
Read more >Python | Sort nested dictionary by key - GeeksforGeeks
This task can be performed using OrderedDict function which converts the dictionary to specific order as mentioned in its arguments manipulated ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I don’t like to make changes “just for fun”. It ordered objects work everywhere, why should we change anything?
I’d prefer to close.