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

@writetome51/alphabetize-by-property

v4.0.0

Published

Re-orders array of objects alphabetically by the value of a particular property

Downloads

9

Readme

alphabetizeByProperty(      property: string,      objects): void

Re-orders objects alphabetically by property.
It coerces the value of each object[property] in objects into a string to
do the sorting. The values of the properties are not modifed.
The sorting algorithm simply compares the unicode value of property in every 2
adjacent objects to decide which should come first, like so:
return (String(aProp) < String(bProp) ? -1 : 1);

Note: in the results, all uppercase letters in the English alphabet come before
all lowercase letters.
Note: property is a string that can include dot-notation
( 'property.subproperty.subsubproperty') . If property is an array index, here
you need to use dot-notation and not square braces. Example:
'1.0' // instead of [1][0]

Examples

let roster = [
	{name: 'Rod Carmichael', group: 'D'},
	{name: 'Todd Garfunkel', group: 'B'},
	{name: 'Rachel Green', group: 'E'},
	{name: 'Mick Jagger', group: 'A'},
	{name: 'Charlie Brown', group: 'D'},
	{name: 'Flip Mavunkel', group: 'C'},
];

alphabetizeByProperty('group', roster);

/************
roster is now
[
	{name: 'Mick Jagger', group: 'A'},
	{name: 'Todd Garfunkel', group: 'B'},
	{name: 'Flip Mavunkel', group: 'C'},
	{name: 'Rod Carmichael', group: 'D'},
	{name: 'Charlie Brown', group: 'D'},
	{name: 'Rachel Green', group: 'E'}
]
************/


roster = [
	{player: {name: 'Rod'}},
	{player: {name: 'Mick'}},
	{player: {name: 'Charlie'}},
	{player: {name: 'Todd'}},
	{player: {name: 'Flip'}},
	{player: {name: 'Rachel'}},
	{player: {name: 'Monica'}},
];

alphabetizeByProperty('player.name', roster);

/************
roster is now
[ 
    { player: { name: 'Charlie' } },
    { player: { name: 'Flip' } },
    { player: { name: 'Mick' } },
    { player: { name: 'Monica' } },
    { player: { name: 'Rachel' } },
    { player: { name: 'Rod' } },
    { player: { name: 'Todd' } }
]
************/


// What about alphabetizing characters with diacriticals?

roster =  [
	{name: 'Rod Carmichael', group: 'Ò'},
	{name: 'Todd Garfunkel', group: 'Í'},
	{name: 'Rachel Green', group: 'I'},
	{name: 'Mick Jagger', group: 'Å'},
	{name: 'Charlie Brown', group: 'O'},
	{name: 'Flip Mavunkel', group: 'A'},
];

alphabetizeByProperty('group', roster);

/************
roster is now
[
    { name: 'Flip Mavunkel', group: 'A' },
    { name: 'Rachel Green', group: 'I' },
    { name: 'Charlie Brown', group: 'O' },
    { name: 'Mick Jagger', group: 'Å' },
    { name: 'Todd Garfunkel', group: 'Í' },
    { name: 'Rod Carmichael', group: 'Ò' } 
]
************/

// What if some of the objects don't have a value for that property,
// or the property is missing?

roster =  [
	{name: 'Rod Carmichael', group: undefined},
	{name: 'Mick Jagger', group: 'Å'},
	{name: 'Charlie Brown', group: 'ZZZ'},
	{name: 'Farley Brown', group: 'Z'},
	{name: 'Rachel Green'},
	{name: 'Charlie Brown', group: null},
	{name: 'Todd Garfunkel', group: undefined},
	{name: 'Flip Mavunkel', group: 'A'},
];

alphabetizeByProperty('group', roster);

/************
roster is now
[
    { name: 'Flip Mavunkel', group: 'A' },
    { name: 'Farley Brown', group: 'Z' },
    { name: 'Charlie Brown', group: 'ZZZ' },
    { name: 'Charlie Brown', group: null }, // null is treated as a string
    { name: 'Rod Carmichael', group: undefined }, // undefined is treated as a string
    { name: 'Rachel Green' }, // missing property is treated as 'undefined'
    { name: 'Todd Garfunkel', group: undefined },
    { name: 'Mick Jagger', group: 'Å' },
]
************/


// Now alphabetize by array index.
// Here we alphabetize by each person's last name,
// which is index 1 of index 1:

let people = [
	['teacher', ['thomas', 'stoppard']],
	['pastor', ['terry', 'blank']],
	['priest', ['sam', 'martin']],
	['mayor', ['amy', 'thomas']],
	['teacher', ['cathy', 'nguyen']],
	['minister', ['ng', 'leung']]
];

alphabetizeByProperty('1.1', people);

/************
people is now
[
	['pastor', ['terry', 'blank']],
	['minister', ['ng', 'leung']],
	['priest', ['sam', 'martin']],
	['teacher', ['cathy', 'nguyen']],
	['teacher', ['thomas', 'stoppard']],
	['mayor', ['amy', 'thomas']]
]
************/

Installation

npm i @writetome51/alphabetize-by-property

Loading

import {alphabetizeByProperty} from '@writetome51/alphabetize-by-property';