# order-by

> order objects in an array by the order their keys occur in another array

Latest version **1.0.0** (published 2014-12-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install order-by
pnpm add order-by
yarn add order-by
bun add order-by
```

Provides the command `order-by`.

## 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.0.0 |
| Published | 2014-12-01 |
| First published | 2014-12-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Erik Garrison |
| Maintainers | ekg |
| Keywords | order, sort, join, utility, command-line |

## Links

- npm: https://www.npmjs.com/package/order-by
- Repository: https://github.com/ekg/order-by
- Issues: https://github.com/ekg/order-by/issues
- npm.io page: https://npm.io/package/order-by

## Dependencies (1)

- [csv-parse](https://npm.io/package/csv-parse.md) 0.0.6

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2014-12-01

## README

# order-by

order the records of one file (or array) by the order of keys in another

```
npm install -g order-by
```

## Usage

``` bash

% cat x
rs4477212       AA
rs3094315       AA
rs3131972       GG
rs12124819      AA
rs11240777      GG

# we'll sort these according to their order in the second file

% cat y
rs11240777
rs12124819
rs3094315
rs3131972
rs4477212

# boom

% order-by x 1 y
rs11240777      GG
rs12124819      AA
rs3094315       AA
rs3131972       GG
rs4477212       AA

```

The method also works on objects, but must be called from javascript:

``` js
// we'd like to sort this by the order in b
var a = [{key:'one',value:'x'},
         {key:'two',value:'x'},
         {key:'two',value:'y'},
         {key:'three',value:'x'},
         {key:'wee',value:'y'},
         {key:'four',value:'x'}]

// our order
var b = ['four','two','three','one']

var orderBy = require('order-by')

// use the value of 'key' in a when sorting according to b
orderBy(a, 'key', b)
/*
[ { key: 'four', value: 'x' },
  { key: 'two', value: 'x' },
  { key: 'two', value: 'y' },
  { key: 'three', value: 'x' },
  { key: 'one', value: 'x' },
  { key: 'wee', value: 'y' } ]
*/
```

Note that elements which aren't in the order which is supplied are sorted at the end (under key `undefined`).

Elements with the same key are sorted at the same position.

## Rationale

This module allows a sorted order in one file to be applied to any other.

## License

MIT

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