# sorts

> Common functions for array.sort()

Latest version **1.1.1** (published 2016-11-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install sorts
pnpm add sorts
yarn add sorts
bun add sorts
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2016-11-04 |
| First published | 2015-09-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 15 |
| Author | Mike MacCana |
| Maintainers | mikemaccana |
| Keywords | algorithms, array, sort, sorting |

## Links

- npm: https://www.npmjs.com/package/sorts
- Repository: https://github.com/mikemaccana/sorts
- Homepage: https://github.com/mikemaccana/sorts#readme
- Issues: https://github.com/mikemaccana/sorts/issues
- npm.io page: https://npm.io/package/sorts

## Recent versions

- 1.1.1 (latest) — 2016-11-04
- 1.1.0 — 2016-10-20
- 1.0.7 — 2016-10-20
- 1.0.6 — 2016-10-20
- 1.0.5 — 2016-04-30
- 1.0.4 — 2015-12-29
- 1.0.3 — 2015-12-16
- 1.0.2 — 2015-09-19
- 1.0.1 — 2015-09-19
- 1.0.0 — 2015-09-14

## README

# Common functions for array.sort(), to keep your code neat and tidy

Tired of staring at the same array.sort() (also known as [Array.prototype.sort()](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/sort) ) functions over and over again?

Worried about stuff like Unicode comparisons?

Need to sort an array of objects by some nested key, using a particular sort function?

Want to do cool stuff like compare domain names, ordering subdomains beneath their parents?

Use the `sorts` module!

## Using the module

	var sorts = require('sorts')

## alphabetical

Sorts by alphabetical order, case insensitive, handles unicode.

Given:

	var fruits = ['pineapple', 'mango', 'coconut']

Just:

	fruits.sort(sorts.alphabetical)

Returns:

	['coconut', 'mango', 'pineapple']

## byLength

Given:

	var fruits = ['pineapple', 'mango', 'coconut']

Returns:

	['mango', 'coconut', 'pineapple']


Just:

	fruits.sort(sorts.byLength)

## byNumber(keyName)
## numeric(keyName)

	var ages = [21, 8, 20]

Just:

	ages.sort(sorts.numeric)

Returns:

	[8, 20, 21]

## domainName

Sort alphabetically, but order subdomains beneath their parents, and include 'www.' immediately after the regular domain name. Eg:

	var domainNames = [
		'banana.com',
		'www.banana.com',
		'peach.com',
		'yolo.swag.banana.com',
		'pear.com',
		'www.pear.com',
		'www.bananameltdown.com',
		'swag.banana.com',
		'bananameltdown.com',
		'www.peach.com'
	]
	domainNames.sort(sorts.domainName)

Returns:

	[
		'banana.com',
		'www.banana.com',
		'swag.banana.com',
		'yolo.swag.banana.com',
		'bananameltdown.com',
		'www.bananameltdown.com',
		'peach.com',
		'www.peach.com',
		'pear.com',
		'www.pear.com'
	]

## byKey(keyName, sortFunction)

Sort an array of objects by a specified key. `sortFunction` can be any of the sort functions above - `alphabetical` is the default `sortFunction`.

	var customers = [{
		name: 'Joe',
		age: 35,
	}, {
		name: 'Alex',
		age: 28
	}]

Just:

	customers.sort(sorts.byKey('name', 'alphabetical'))

Returns:

	[{
		name: 'Alex',
		age: 28
	},{
		name: 'Joe',
		age: 35,
	}]

# Please add more sorts!

---
_Source: https://npm.io/package/sorts · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
