npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

sort-object-properties

v0.3.3

Published

Utility that return copy of object with sorted keys

Readme

sort-object-properties

Utility that return copy of object with sorted keys

Installation

npm install sort-object-properties --save

Usage

Import

//ES6
import sort from 'sort-object-properties';
//CommonJS
var sort = require('sort-object-properties');

Simple call

const objectWithUnsortedKeys = {
    d: 'value1',
    c: 'value2',
    e: 'value3',
    b: 'value4',
    a: 'value5'
};

var sortedObject = sort(objectToSort);

console.log(sortedObject); 
/*
    a: 'value5',
    b: 'value4',
    c: 'value2',
    d: 'value1',
    e: 'value3'
*/

API

Global function

By default sorts object ascending by its keys. Use second parameter if need more flexibility.

| Argument | Type | Optional | Description |
| ------------- | :---: | :------: | :--------------------------- | | obj | Object | false | Object to sort | | sortFunction | SortFunction | true | Function used to sort object |


SortFunction

| Argument | Type | Optional | Description | | ---------- | :--------------: | :------: | :---------- | | a | PropertyObject | true | Left side property | | b | PropertyObject | true | Right side property |


PropertyObject

| Property | Type | Optional | Description | | ---------- | :--------------------------: | :------: | :------------------------ | | key | string, number, Symbol | true | Key used in parent object | | value | any | true | Value assigned to key |

value function

Sorts object properties by its values.

| Argument | Type | Optional | Description | | ------------- | :-----------------------: | :------: | :---------- | | obj | Object | false | Object to sort | | direction | SortDirection, Number | true | Sort direction | | valueSelector | Function | true | Function that return simple value from complex type properties |

key function

Sorts object properties by its keys.

| Argument | Type | Optional | Description | | ---------- | :-----------------------: | :------: | :--------------- | | obj | PropertyObject | false | Object to sort | | direction | SortDirection, Number | true | Sort direction |

sortDirection constant

Constant with sort directions, use that const or just use plain numbers in function calls.

| Name | Value | | ---- | --- | | ascending | 1 | | descending | -1 |

Examples

Sort by values using global function

const objectWithUnsortedValues = {
    key1: 'd',
    key2: 'a',
    key3: 'e',
    key4: 'b',
    key5: 'c'
};

//ES6 syntax
var sortedObject = sort(objectWithUnsortedValues, ({value: valueA}, {value: valueB}) => valueA > valueB);
//ES5 syntax
var sortedObject = sort(objectWithUnsortedValues, function (a, b){ return a.value > b.value;});

console.log(sortedObject);
/*
    key2: 'a',
    key4: 'b',
    key5: 'c'
    key1: 'd',
    key3: 'e',
*/

Sort by values using values function

import { value as sortByValue } from 'sort-object-properties';

const objectWithUnsortedValues = {
    key1: 'd',
    key2: 'a',
    key3: 'e',
    key4: 'b',
    key5: 'c'
};

var sortedObject = sortByValue(objectWithUnsortedValues);

console.log(sortedObject);
/*
    key2: 'a',
    key4: 'b',
    key5: 'c'
    key1: 'd',
    key3: 'e',
*/

Sort by keys using key function

import { key as sortByKey, sortDirection } from 'sort-object-properties';

const objectWithUnsortedKeys = {
    key4: 'e',
    key1: 'c',
    key3: 'c',
    key2: 'd',
    key5: 'a'
};

var sortedObject = sortByKey(objectWithUnsortedKeys, sortDirection.descending);

console.log(sortedObject);
/*
    key5: 'a',
    key4: 'e',
    key3: 'c',
    key2: 'd',
    key1: 'c'
*/