Sorts an array by key, maintaining key to data correlations. This is useful mainly for associative arrays. This is a implementation of PHP ksort() without any sort_flags.
To convert in object/array into associative array, please see object-to-associative-array
npm install key-sort
NODE v0.8.0 or higher
- Input should always be an associative array.
- Provided array should contain objects and each object should have only one property.
- Property value can be either primitive data types or another associative array.
- Returns array sorted by property name.
- Also modifies input array.
- Deep sorting is default.
- Pass false for not nesting sort.
const keySort = require('key-sort');
let arr = [
{world: 'world'},
{middle: 'hello'},
{greeting: 'Welcome to'}
];
let sortedArr = keySort(arr);
// [{greeting: 'Welcome to'},{middle: 'hello'},{world: 'world'}]
const keySort = require('key-sort');
let arr = [
{x: 1},
{a: [
{f: 365},
{d: 25}
]}
];
let sortedArr = keySort(arr);
// [{"a" :[{"d" :25},{"f" :365}]},{"x" :1}]
const keySort = require('key-sort');
let arr = [
{x: 1},
{a: [
{f: 365},
{d: 25}
]}
];
let sortedArr = keySort(arr, false);
// [{"a" :[{"f" :365},{"d" :25}]},{"x" :1}]
N/A
Original Author