question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Define different sort order based on nested objects

See original GitHub issue

In 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:

  1. 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
  1. 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!
  1. 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:closed
  • Created 5 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
puzrincommented, Dec 3, 2018

I also remembered that sorting was guaranteed, but it turns out it is an implementation detail, not a language definition. Thus not guaranteed (as you can see in the above pdf)

I don’t like to make changes “just for fun”. It ordered objects work everywhere, why should we change anything?

0reactions
puzrincommented, Dec 4, 2018

I’d prefer to close.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found