What is sortobject?
The sortobject npm package is a utility for sorting the keys of an object. It can be used to ensure consistent key ordering, which can be useful for tasks such as comparing objects, generating consistent JSON output, and more.
What are sortobject's main functionalities?
Sort object keys alphabetically
This feature sorts the keys of an object in alphabetical order. The code sample demonstrates how to use the sortobject package to sort the keys of an object.
const sortobject = require('sortobject');
const obj = { b: 2, a: 1, c: 3 };
const sortedObj = sortobject(obj);
console.log(sortedObj); // { a: 1, b: 2, c: 3 }
Sort nested object keys
This feature sorts the keys of nested objects as well. The code sample shows how nested objects are sorted by their keys.
const sortobject = require('sortobject');
const obj = { b: { d: 4, c: 3 }, a: 1 };
const sortedObj = sortobject(obj);
console.log(sortedObj); // { a: 1, b: { c: 3, d: 4 } }
Other packages similar to sortobject
sort-keys
The sort-keys package sorts the keys of an object in a similar manner to sortobject. It offers additional options such as sorting in reverse order and custom sorting functions. Compared to sortobject, sort-keys provides more flexibility in how the keys are sorted.
deep-sort-object
The deep-sort-object package sorts the keys of an object deeply, including nested objects. It is similar to sortobject in that it handles nested objects, but it also provides more control over the sorting process, such as custom sorting functions.
Sort Object
![Wishlist browse button](https://img.shields.io/badge/wishlist-donate-yellow.svg)
Returns a copy of an object, sorted deeply by its keys, without mangling any arrays inside of it
Install
- Install:
npm install --save sortobject
- Require:
require('sortobject')
<script type="module">
import * as pkg from '//dev.jspm.io/sortobject'
</script>
This package is published with the following editions:
sortobject
aliases sortobject/source/index.js
sortobject/source/index.js
is esnext source code with require for modulessortobject/edition-browsers/index.js
is esnext compiled for browsers with require for modules
This project provides its type information via inline JSDoc Comments. To make use of this in TypeScript, set your maxNodeModuleJsDepth
compiler option to 5
or thereabouts. You can accomlish this via your tsconfig.json
file like so:
{
"compilerOptions": {
"maxNodeModuleJsDepth": 5
}
}
Usage
API Documentation.
var sortObject = require('sortobject')
var fixture = {
c: true,
a: true,
b: null,
d: [
{
c: true,
a: true,
b: null
},
{
c: true,
a: true,
b: null,
d: [
{
c: true,
a: true,
b: null
}
]
}
]
}
var actual = sortObject(fixture)
console.log(JSON.stringify(actual, null, 4))
History
Discover the release history by heading on over to the HISTORY.md
file.
Contribute
Discover how you can contribute by heading on over to the CONTRIBUTING.md
file.
Backers
Maintainers
These amazing people are maintaining this project:
These amazing people have contributed finances to this project:
Become a sponsor!
![Wishlist browse button](https://img.shields.io/badge/wishlist-donate-yellow.svg)
Contributors
These amazing people have contributed code to this project:
Discover how you can contribute by heading on over to the CONTRIBUTING.md
file.
License
Unless stated otherwise all works are:
and licensed under: