# compare-func

> Get a compare function for array to sort

Latest version **2.0.0** (published 2020-05-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install compare-func
pnpm add compare-func
yarn add compare-func
bun add compare-func
```

## Health

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

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2020-05-15 |
| First published | 2015-04-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/compare-func) |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 4.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 16 |
| Author | Steve Mao |
| Maintainers | stevemao |
| Keywords | compare-func, arr, array, by, compare, dot, get, obj, object, prop, property, sort, sorting |

## Links

- npm: https://www.npmjs.com/package/compare-func
- Repository: https://github.com/stevemao/compare-func
- Issues: https://github.com/stevemao/compare-func/issues
- npm.io page: https://npm.io/package/compare-func

## Dependencies (2)

- [dot-prop](https://npm.io/package/dot-prop.md) ^5.1.0
- [array-ify](https://npm.io/package/array-ify.md) ^1.0.0

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2020-05-15
- 1.3.4 — 2020-05-15
- 1.3.3 — 2020-05-15
- 1.3.2 — 2016-06-30
- 1.3.1 — 2015-06-14
- 1.3.0 — 2015-06-13
- 1.2.0 — 2015-05-04
- 1.1.0 — 2015-05-02
- 1.0.2 — 2015-04-03
- 1.0.1 — 2015-04-03
- 1.0.0 — 2015-04-03
- 0.0.0 — 2015-04-03

## README

#  [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage Status][coveralls-image]][coveralls-url]

> Get a compare function for array to sort


## Install

```sh
$ npm install --save compare-func
```


## Usage

```js
var compareFunc = require('compare-func');

// sort by an object property
[{x: 'b'}, {x: 'a'}, {x: 'c'}].sort(compareFunc('x'));
//=> [{x: 'a'}, {x: 'b'}, {x: 'c'}]

// sort by a nested object property
[{x: {y: 'b'}}, {x: {y: 'a'}}].sort(compareFunc('x.y'));
//=> [{x: {y: 'a'}}, {x: {y: 'b'}}]

// sort by the `x` propery, then `y`
[{x: 'c', y: 'c'}, {x: 'b', y: 'a'}, {x: 'b', y: 'b'}].sort(compareFunc(['x', 'y']));
//=> [{x: 'b', y: 'a'}, {x: 'b', y: 'b'}, {x: 'c', y: 'c'}]

// sort by the returned value
[{x: 'b'}, {x: 'a'}, {x: 'c'}].sort(compareFunc(function(el) {
  return el.x;
}));
//=> [{x: 'a'}, {x: 'b'}, {x: 'c'}]
```


## API

### compareFunc([property])

Returns a compare function for array to sort

#### property

Type: `string`, `function` or `array` of either

If missing it sorts on itself.

The string can be a [dot path](https://github.com/sindresorhus/dot-prop) to a nested object property.


## Related

- [sort-on](https://github.com/sindresorhus/sort-on) - Sort an array on an object property


## License

MIT © [Steve Mao](https://github.com/stevemao)


[npm-image]: https://badge.fury.io/js/compare-func.svg
[npm-url]: https://npmjs.org/package/compare-func
[travis-image]: https://travis-ci.org/stevemao/compare-func.svg?branch=master
[travis-url]: https://travis-ci.org/stevemao/compare-func
[daviddm-image]: https://david-dm.org/stevemao/compare-func.svg?theme=shields.io
[daviddm-url]: https://david-dm.org/stevemao/compare-func
[coveralls-image]: https://coveralls.io/repos/stevemao/compare-func/badge.svg
[coveralls-url]: https://coveralls.io/r/stevemao/compare-func

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