
Product
Introducing Socket Firewall Enterprise: Flexible, Configurable Protection for Modern Package Ecosystems
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.
sort-by-property
Advanced tools
Type-safe array sorting method with support for deeply nested properties and Typescript autocompletion.
Type-safe array sorting method with support for deeply nested properties and Typescript autocompletion.
blogPosts.sort(sortByProperty('author.name', 'asc'));
"author.name"string, number, boolean, Date, Symbol and BigInt valuesnull and undefined values gracefully by moving the object to the end of the arrayTry it out: https://codesandbox.io/s/sort-by-property-example-hin358
Requires Typescript 4.1+ because of the internal use of template literals for the autocompletion.
Install with your favorite package manager:
npm install sort-by-property
or
yarn add sort-by-property
// For an array of objects
import { sortByProperty } from 'sort-by-property';
// For one-dimensional arrays
import { sortBy } from 'sort-by-property';
import { sortByProperty } from 'sort-by-property';
interface BlogPost {
id: number;
title: string;
author: {
id: number;
name: string;
};
}
const blogPosts: BlogPost[] = [
{
id: 1,
title: 'Never gonna run around and desert you',
author: {
id: 10,
name: 'Joe',
},
},
{
id: 2,
title: 'Never gonna let you down',
author: {
id: 20,
name: 'Ben',
},
},
{
id: 3,
title: 'Never gonna give you up',
author: {
id: 30,
name: 'Alice',
},
},
];
// Sort the blog posts by author name
blogPosts.sort(sortByProperty('author.name', 'asc'));
// If you need to use a custom locale for sorting strings, you can do
blogPosts.sort(sortByProperty('author.name', 'asc', {locale: 'nb-no'}))
Will sort the array ascending by author.name:
[
{ id: 3, title: 'Never gonna give you up', author: { id: 30, name: 'Alice' } },
{ id: 2, title: 'Never gonna let you down', author: { id: 20, name: 'Ben' } },
{ id: 1, title: 'Never gonna run around and desert you', author: { id: 10, name: 'Joe' } },
];
Will show a type error when you try to sort on properties that do not exist:

Will show an autocomplete of the available properties to sort on:

This package exports 2 methods. Use sortBy to sort one-dimensional arrays. This sorting method supports all the same types as sortByProperty.
import { sortBy } from 'sort-by-property';
const array = ['c', 'b', 'a'];
array.sort(sortBy('asc'));
// Result: ['a', 'b', 'c']
* on an array with 10 million items: ~450ms vs ~1350ms. See the /src/examples directory.
FAQs
Type-safe array sorting method with support for deeply nested properties and Typescript autocompletion.
The npm package sort-by-property receives a total of 1,398 weekly downloads. As such, sort-by-property popularity was classified as popular.
We found that sort-by-property demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.

Security News
Open source dashboard CNAPulse tracks CVE Numbering Authorities’ publishing activity, highlighting trends and transparency across the CVE ecosystem.

Product
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.