# sort-objects-array

> Simple module to sort an array of objects by a property without mutating the original array.

Latest version **5.0.1** (published 2026-11-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install sort-objects-array
pnpm add sort-objects-array
yarn add sort-objects-array
bun add sort-objects-array
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 5.0.1 |
| Published | 2026-11-01 |
| First published | 2018-03-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=22.0.0 |
| Dependencies | 0 |
| Unpacked size | 18 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 5 |
| Author | Juan Convers |
| Maintainers | webdacjs |
| Keywords | sort, array, object, order |

## Links

- npm: https://www.npmjs.com/package/sort-objects-array
- Repository: https://github.com/webdacjs/sort-objects-array
- Homepage: https://github.com/webdacjs/sort-objects-array#readme
- Issues: https://github.com/webdacjs/sort-objects-array/issues
- npm.io page: https://npm.io/package/sort-objects-array

## Recent versions

- 5.0.1 (latest) — 2026-11-01
- 4.1.0 — 2026-02-03
- 4.0.0 — 2025-04-25
- 3.0.6 — 2025-04-24
- 3.0.5 — 2025-04-24
- 3.0.4 — 2024-07-29
- 3.0.3 — 2024-07-29
- 3.0.2 — 2024-07-29
- 3.0.1 — 2024-03-07
- 3.0.0 — 2024-03-07
- 2.0.0 — 2023-03-07
- 1.7.1 — 2020-01-03
- 1.7.0 — 2019-08-12
- 1.5.0 — 2019-01-17
- 1.4.1 — 2018-07-22
- … 8 more at https://npm.io/package/sort-objects-array/versions

## README

# sort-objects-array

This tiny and fast module performs the sort of an Array of objects with the property required. The module returns a new copy of the array so the original is not mutated. The module even supports the sort objects (but they get transformed into an array of objects first)

This module only has dev dependencies (`vitest`, `tsup`, `typescript`) to run the tests and develop in ts.

## Install

You can install it with npm:

```sh
$ npm install --save sort-objects-array
```
## Usage

The module requires two parameters: The array to sort and the property to use in the sorting:

```ts
// Example1: Sorting the countries by the name

import { sortObjArr } from 'sort-objects-array';

interface Country {
    name: string;
    code: string;
    area: number;
}

const countries: Country[] = [
    { name: 'Colombia', code: 'co', area: 1197411 },
    { name: 'Argentina', code: 'ar', area: 2766890 },
    { name: 'Brasil', code: 'br', area: 8511965 },
    { name: 'canada', code: 'ca', area: 9984670 }
];

sortObjArr(countries, 'name');
// Returns
// [ { name: 'Argentina', code: 'ar', area: 2766890 },
//   { name: 'Brasil', code: 'br', area: 8511965 },
//   { name: 'Colombia', code: 'co', area: 1197411 },
//   { name: 'canada', code: 'ca', area: 9984670 } ]
```

The default order is **ascending** but it's possible to change the order to descending using *'desc'* or *'reverse'* keyword as a third parameter:

```ts

// Example2: Sorting the countries by the largest area

sortObjArr(countries, 'area', 'desc');
// Returns
// [ { name: 'canada', 'code': 'ca', 'area': 9984670},
//   { name: 'Brasil', code: 'br', area: 8511965 },
//   { name: 'Argentina', code: 'ar', area: 2766890 },
//   { name: 'Colombia', code: 'co', area: 1197411 } ]
```

If you prefer you can pass the order as a configuration object using the **order**
property:

```ts
// Example3: Sorting the countries by the largest area using config object.
sortObjArr(countries, 'area', {order: 'desc'});
// Returns
// [ { name: 'canada', 'code': 'ca', 'area': 9984670},
//   { name: 'Brasil', code: 'br', area: 8511965 },
//   { name: 'Argentina', code: 'ar', area: 2766890 },
//   { name: 'Colombia', code: 'co', area: 1197411 } ]
```

By default, the module respects the case sensitivity of the values. If you want to override it and ignore it you can pass the flag ***caseinsensitive*** in the configuration object.

```ts

// Example4: Sorting by name. Case insensitive

sortObjArr(countries, 'name', {caseinsensitive: true});
// Returns
// [ { name: 'Argentina', code: 'ar', area: 2766890 },
//   { name: 'Brasil', code: 'br', area: 8511965 },
//   { name: 'canada', 'code': 'ca', 'area': 9984670},
//   { name: 'Colombia', code: 'co', area: 1197411 } ]

```
Or both:

```ts

// Example5: Sorting by name desc and case insensitive

sortObjArr(countries, 'name', {order: 'desc', caseinsensitive: true});
// Returns
// [ { name: 'Colombia', code: 'co', area: 1197411 },
//   { name: 'canada', 'code': 'ca', 'area': 9984670},
//   { name: 'Brasil', code: 'br', area: 8511965 },
//   { name: 'Argentina', code: 'ar', area: 2766890 } ]

```

It can also sort objects, converting them first to an array of objects using 'key' and 'value' in each item:

```ts

// Example6: Sorting by an object by value

const countriesObj = {
    Colombia: 1197411,
    Argentina: 2766890,
    canada: 9984670,
    NonExistingCountry: null,
    Brasil: 8511965
}

sortObjArr(countriesObj, 'value', {order: 'desc'});
// Returns
// [ { key: 'canada', value: 9984670 },
//   { key: 'Brasil', value: 8511965 },
//   { key: 'Argentina', value: 2766890 },
//   { key: 'Colombia', value: 1197411 },
//   { key: 'NonExistingCountry', value: null } ]

```

Finally it can also sort an array of arrays, flattening or not based on your needs:

```ts

// Example7: Sorting the countries im an array of arrays

const countries = [{'name': 'Colombia', 'code': 'co', 'area': 1197411},
                   {'name': 'Argentina', 'code': 'ar', 'area': 2766890},
                   {'name': 'Brasil', 'code': 'br', 'area': 8511965},
                   {'name': 'canada', 'code': 'ca', 'area': 9984670}]

const countriesCountries = [countries, countries]

sortObjArr(countriesCountries, 'name');
// Returns
// [ [{ name: 'Argentina', code: 'ar', area: 2766890 }, ...],
//   [{ name: 'Argentina', code: 'ar', area: 2766890 }, ...]
// ]

// Example8: Sorting the countries im an array of arrays flattening them

sortObjArr(countriesCountries, 'area', { flatten: true, order: 'desc' });
// Returns
// [ { name: 'canada', 'code': 'ca', 'area': 9984670},
//   { name: 'canada', 'code': 'ca', 'area': 9984670},
//   ...
// ]

```

## Development

### Building
```bash
npm run build
```

### Testing
```bash
# Run unit tests
npm test

# Run tests in watch mode
npm run test:watch

# Run tests with coverage
npm run test:coverage

# Run integration tests (verifies both CJS and ESM compatibility)
npm run test:integration
```

## Why This Package Has Dual Module Support

This package is configured to work seamlessly with both CommonJS and ES Modules:

- **CommonJS**: The traditional Node.js module format using `require()`
- **ES Modules**: The standard JavaScript module format using `import/export`

This dual compatibility ensures the package can be used in any project, regardless of which module system it uses.

### License

Copyright © 2025, [Juan Convers](https://www.juanconvers.com).
Released under the [MIT License](LICENSE).

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