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 🙏

© 2025 – Pkg Stats / Ryan Hefner

dhruvtodo

v2.0.60

Published

A comprehensive todo management library with task and category utilities, plus array sorting functions

Readme

Object Sort Utils

A lightweight utility library for sorting arrays of objects by any key with support for both ascending and descending order.

Installation

npm install object-sort-utils

Usage

const sortByKey = require("object-sort-utils");

// Example data
const users = [
  { name: "John", age: 30, score: 85 },
  { name: "Alice", age: 25, score: 92 },
  { name: "Bob", age: 35, score: 78 },
];

// Sort by age (ascending - default)
const sortedByAge = sortByKey(users, "age");
console.log(sortedByAge);
// Output: [{ name: 'Alice', age: 25, score: 92 }, { name: 'John', age: 30, score: 85 }, { name: 'Bob', age: 35, score: 78 }]

// Sort by score (descending)
const sortedByScore = sortByKey(users, "score", "desc");
console.log(sortedByScore);
// Output: [{ name: 'Alice', age: 25, score: 92 }, { name: 'John', age: 30, score: 85 }, { name: 'Bob', age: 35, score: 78 }]

// Sort by name (ascending)
const sortedByName = sortByKey(users, "name");
console.log(sortedByName);
// Output: [{ name: 'Alice', age: 25, score: 92 }, { name: 'Bob', age: 35, score: 78 }, { name: 'John', age: 30, score: 85 }]

API

sortByKey(array, key, order)

Sorts an array of objects by the value of a specified key.

Parameters

  • array (Array): Array of objects to sort
  • key (string): Key to sort by
  • order (string, optional): Sort order - 'asc' for ascending (default) or 'desc' for descending

Returns

  • (Array): A new sorted array (original array is not modified)

Error Handling

The function throws errors for invalid inputs:

  • If the first parameter is not an array
  • If the key is not a string
  • If the order is not 'asc' or 'desc'

Handling of Special Values

  • undefined values are placed at the end of the sorted array
  • null values are placed at the end of the sorted array
  • The function works with numbers, strings, and other comparable types

Examples

Sorting Numbers

const products = [
  { name: "Laptop", price: 999 },
  { name: "Phone", price: 599 },
  { name: "Tablet", price: 299 },
];

const cheapToExpensive = sortByKey(products, "price");
const expensiveToCheap = sortByKey(products, "price", "desc");

Sorting Strings

const books = [
  { title: "Zebra Adventures", author: "John Doe" },
  { title: "Apple Stories", author: "Jane Smith" },
  { title: "Mountain Tales", author: "Bob Johnson" },
];

const alphabeticalByTitle = sortByKey(books, "title");
const alphabeticalByAuthor = sortByKey(books, "author");

Handling Missing Values

const data = [
  { name: "John", age: 30 },
  { name: "Alice" }, // missing age
  { name: "Bob", age: 25 },
];

const sorted = sortByKey(data, "age");
// Objects with missing 'age' will be placed at the end

Features

  • ✅ Lightweight and fast
  • ✅ No dependencies
  • ✅ Supports both ascending and descending sort
  • ✅ Handles edge cases (null, undefined values)
  • ✅ Doesn't modify the original array
  • ✅ Full error handling
  • ✅ Works with Node.js 12+
  • ✅ TypeScript-friendly

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Changelog

1.0.0

  • Initial release
  • Basic sorting functionality for arrays of objects
  • Support for ascending and descending order
  • Error handling for invalid inputs