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.

Rule to sort properties in object literal

See original GitHub issue

Similar to sort-vars, how about a rule to always sort object properties alphabetically when defining an object literal?

It would be really useful for things like options objects. I realize that the property order can matter in iteration, but it’s nothing you should rely on anyway. With the new ES6 data structures, I think the use case for object literals is shrinking to be a lot about options objects, including in native APIs.

You could argue that it will cause a huge number of warnings in existing code and thus not be useful at all. I’m not completely sure, I just know that I often use vim’s sort command, but it doesn’t work well if a property value spans several lines.

Warnings:

var obj = {
  c: "foo",
  a: "bar",
  b: {
    e: 1,
    d: 2,
  }
};

OK:

var obj = {
  a: "bar",
  b: {
    d: 2,
    e: 1,
  },
  c: "foo",
};

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Gopikrishna19commented, Aug 17, 2016

http://eslint.org/docs/rules/sort-keys from eslint 3.3.0 on 😃

1reaction
gajuscommented, Apr 18, 2016

@nzakas Now that https://github.com/jacobrask/eslint-plugin-sorting has somewhat matured, is it worth it to consider adding it to the core?

Read more comments on GitHub >

github_iconTop Results From Across the Web

object-literal-sort-keys - Rule
Checks ordering of keys in object literals. When using the default alphabetical ordering, additional blank lines may be used to group object properties...
Read more >
sort-keys - ESLint - Pluggable JavaScript Linter
This rule checks all property definitions of object expressions and verifies that all variables are sorted alphabetically.
Read more >
How to Sort a JS Object Literal? - Stack Overflow
Object properties are in no specific order (the order is implementation dependent) and you cannot sort the properties.
Read more >
Property order is predictable in JavaScript objects since ES2015
The order of properties in an object depends on the type of the included properties and their values.
Read more >
Working with objects - JavaScript - MDN Web Docs - Mozilla
Each property name before colons is an identifier (either a name, a number, or a string literal), and each valueN is an expression...
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