Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/writetome51/alphabetize-by-property
Re-orders array of objects alphabetically by the value of a particular property
https://github.com/writetome51/alphabetize-by-property
alphabetical alphabetizer javascript objects sort
Last synced: 7 days ago
JSON representation
Re-orders array of objects alphabetically by the value of a particular property
- Host: GitHub
- URL: https://github.com/writetome51/alphabetize-by-property
- Owner: writetome51
- License: mit
- Created: 2019-01-31T04:56:55.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-08-16T01:32:49.000Z (over 4 years ago)
- Last Synced: 2024-12-09T14:47:42.371Z (about 1 month ago)
- Topics: alphabetical, alphabetizer, javascript, objects, sort
- Language: JavaScript
- Size: 22.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# alphabetizeByProperty(
property: string,
objects
): voidRe-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
```js
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
```bash
npm i @writetome51/alphabetize-by-property
```
## Loading
```js
import {alphabetizeByProperty} from '@writetome51/alphabetize-by-property';
```