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

als-smart-sort

v1.0.0

Published

A versatile sorting function for JavaScript arrays, handling various data types and nested properties.

Downloads

9

Readme

als-smart-sort

A versatile sorting function for JavaScript arrays, als-smart-sort provides the capability to sort arrays based on various data types, including nested properties. This function is designed to handle arrays of objects with ease, sorting by numeric, string, or date properties, while also dealing with nested object properties.

Installation

npm install als-smart-sort

Function Parameters

  • array (Array): The array to be sorted.
  • propPath (String): The path to the property used for sorting. Nested properties can be accessed with dot notation (e.g., prop.subprop).
  • asc (Boolean): Optional. Determines the sorting order. Set to true for ascending order (default) or false for descending order.

Note: The function performs type validation and returns an empty array if the input is invalid.

Browser support

<script src="node_modules/als-smart-sort/sort.js"></script>

Usage

Here are some examples of how als-smart-sort can be used:

Basic Sorting

const smartSort = require('als-smart-sort');

// Sorting an array of objects by a numeric property
const numericArray = [{ value: 3 }, { value: 1 }, { value: 2 }];
console.log(smartSort(numericArray, 'value')); // [{ value: 1 }, { value: 2 }, { value: 3 }]

// Sorting by a string property in descending order
const stringArray = [{ name: 'Bob' }, { name: 'Alice' }, { name: 'Charlie' }];
console.log(smartSort(stringArray, 'name', false)); // [{ name: 'Charlie' }, { name: 'Bob' }, { name: 'Alice' }]

Sorting with Nested Properties

// Sorting an array of objects with nested properties
const nestedArray = [{ data: { score: 10 } }, { data: { score: 5 } }, { data: { score: 7 } }];
console.log(smartSort(nestedArray, 'data.score')); // [{ data: { score: 5 } }, { data: { score: 7 } }, { data: { score: 10 } }]

Sorting Mixed Number and String Representations

// Sorting an array with mixed number and string representations
const mixedArray = [{ value: '10' }, { value: 5 }, { value: '2' }];
console.log(smartSort(mixedArray, 'value')); // [{ value: '2' }, { value: 5 }, { value: '10' }]

Sorting by date

const array = [
   { event: 'C', date: new Date('2022-01-03') },
   { event: 'A', date: new Date('2022-01-01') },
   { event: 'B', date: new Date('2022-01-02') }
];
const sorted = smartSort(array, 'date');
console.log(sorted)

Output:

[
  { event: 'A', date: 2022-01-01T00:00:00.000Z },
  { event: 'B', date: 2022-01-02T00:00:00.000Z },
  { event: 'C', date: 2022-01-03T00:00:00.000Z }
]