# lodash-keyarrange

> Lodash plugin to correctly sort the keys within an object

Latest version **2.0.1** (published 2025-09-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install lodash-keyarrange
pnpm add lodash-keyarrange
yarn add lodash-keyarrange
bun add lodash-keyarrange
```

## Health

**Score 30/100 (F)** — status: maintenance-mode.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2025-09-03 |
| First published | 2015-06-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Dependencies | 1 |
| Unpacked size | 4.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Matt Carter |
| Maintainers | hash-bang |
| Keywords | keys, lodash, object, sort |

## Links

- npm: https://www.npmjs.com/package/lodash-keyarrange
- Repository: https://github.com/hash-bang/lodash-keyarrange
- Homepage: https://github.com/hash-bang/lodash-keyarrange#readme
- Issues: https://github.com/hash-bang/lodash-keyarrange/issues
- npm.io page: https://npm.io/package/lodash-keyarrange

## Dependencies (1)

- [chai](https://npm.io/package/chai.md) ^6.0.1

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 2.0.1 (latest) — 2025-09-03
- 2.0.0 — 2025-02-20
- 1.1.1 — 2019-06-17
- 1.1.0 — 2015-08-27
- 1.0.0 — 2015-06-18

## README

lodash-keyarrange
=================
Lodash plugin to correctly sort the keys within an object.


Why
---

In JavaScript objects keys are generally stored in the order in which they are created. [ES6 formalizes this behaviour](http://www.ecma-international.org/ecma-262/6.0/#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys).

	var a = {};
	a.foo = 1;
	a.bar = 2;
	a.baz = 3;
	// a = { foo: 1, bar: 2, baz: 3 }

But sometimes we need to output the object with the keys correctly sorted.

This lodash mixin allows key rearrangement within an object - returning a new object.

You can also deep sort an object with `keyArrangeDeep()`.


Example usage
-------------

	var _ = require('lodash')
		.mixin(require('lodash-keyarrange'));

	var a = {foo: 'fooValue', bar: 'barValue', baz: 'bazValue'};

	console.log( _.keyArrange(a) );
	// Outputs { bar: 'barValue', baz: 'bazValue', foo: 'fooValue' }


You can also provide a custom sorter for the keys:

	// Example of reverse sorting keys
	var obj = {foo: 'fooValue', bar: 'barValue', baz: 'bazValue'};

	console.log( _.keyArrange(obj, function(a, b) {
		if (a < b) {
			return 1;
		} else if (b < a) {
			return -1;
		} else {
			return 0;
		}
	} ) );

	// Outputs { foo: 'fooValue', baz: 'bazValue', bar: 'barValue'  }


keyArrangeDeep()
----------------
It is also possible to traverse an object and arrange all keys. Scalars and array types are left unaltered but all objects will be run via the keyArrange function.

For Example:

	var _ = require('lodash')
		.mixin(require('lodash-keyarrange'));

	var out = _.keyArrangeDeep({
		gamma: [
			{
				omegaValue: 123,
				gammaFoo: 'gammaFooValue',
			},
			{
				kappa: {
					gammaKappaBaz: 'hello',
				},
			},
			1337,
		],
		alpha: 123,
		si: {
			theta: 999,
			eta: 'value!',
		},
		beta: 'hello world',
	});

	console.log(JSON.stringify(out, null, "\t"));

Will output:

	{
		"alpha": 123,
		"beta": "hello world",
		"gamma": [
			{
				"gammaFoo": "gammaFooValue",
				"omegaValue": 123
			},
			{
				"kappa": {
					"gammaKappaBaz": "hello"
				}
			},
			1337
		],
		"si": {
			"eta": "value!",
			"theta": 999
		}
	}

---
_Source: https://npm.io/package/lodash-keyarrange · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
