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 🙏

© 2024 – Pkg Stats / Ryan Hefner

sort-by-object

v1.0.2

Published

This module offers advanced sorting capabilities for arrays of objects, supporting sorting by deeply nested object properties. Users can specify the attribute path and choose between ascending or descending order. It handles both numeric and string values

Downloads

14

Readme

📦 sort-by-object

sort-by-object is a utility function that allows sorting arrays of objects based on nested properties. It supports sorting both strings and numbers, and allows specifying the sort order (asc or desc). Additionally, you can control whether the original array is mutated or if a new sorted array is returned.

🚀 Installation

To install sort-by-object in your project, run the following command:

npm install sort-by-object

📖 Usage

Here’s how to use sort-by-object in your TypeScript/JavaScript project.

Example Code

import { sortByObject } from 'sort-by-object';

const data = [
  { name: 'John', age: 25 },
  { name: 'Alice', age: 30 },
  { name: 'Bob', age: 22 },
];

// Sort by 'age' in ascending order and mutate the original array (default)
sortByObject(data, { keyPath: ['age'], order: 'asc' });

// Sort by 'name' without mutating the original array (creates a copy)
const sortedByName = sortByObject(data, { keyPath: ['name'], order: 'asc', mutate: false });
console.log(sortedByName);

Nested Key Example

const nestedData = [
  { user: { info: { name: 'John' } }, score: 10 },
  { user: { info: { name: 'Alice' } }, score: 15 },
  { user: { info: { name: 'Bob' } }, score: 5 },
];

// Sort by nested 'user.info.name' in ascending order without mutating the original array
const sortedByNestedName = sortByObject(nestedData, { keyPath: ['user', 'info', 'name'], order: 'asc', mutate: false });
console.log(sortedByNestedName);

🛠️ Methods

Function Signature

function sortByObject<T>(array: T[], options: SortOptions): T[];
  • array (T[]): The array of objects to be sorted.
  • options (SortOptions): An object containing the sorting options.
    • keyPath (Array<string | number | symbol>): The path to the nested key you want to sort by.
    • order (SortOrder): Defines whether to sort in ascending ('asc') or descending ('desc') order (optional, default is 'asc').
    • mutate (boolean): If true, the original array is sorted in place. If false, a new sorted array is returned (optional, default is true).

Type Definitions

type SortOrder = 'asc' | 'desc';

interface SortOptions {
  keyPath: (string | number | symbol)[];
  order?: SortOrder;
  mutate?: boolean;
}

🔧 Options

  • keyPath (Array<string | number | symbol>): The path to the nested key you want to sort by.
  • order (SortOrder): Defines whether to sort in ascending ('asc') or descending ('desc') order (default is 'asc').
  • mutate (boolean): If true, sorts the array in place. If false, returns a new sorted array (default is true).

📌 Notes

This utility is ideal for scenarios where you need to sort arrays of complex objects with deeply nested properties. It handles both string and numeric comparisons.

🤝 Contributing

Contributions are welcome! If you have suggestions or improvements, feel free to submit a pull request or open an issue on the project’s GitHub repository.

📄 License

This project is licensed under the MIT License. See the LICENSE file for more details.

👤 Author

Khalil MEQQORI - Creator of the module
For any questions or issues, please open an issue on the GitHub repository or contact at [email protected].