# sort-object-keys2

> 對物件的鍵進行排序，支援自訂排序函式、指定鍵順序和原地修改選項 / Sort object keys with custom sort functions, specified key order, and in-place modification options

Latest version **3.0.8** (published 2026-03-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install sort-object-keys2
pnpm add sort-object-keys2
yarn add sort-object-keys2
bun add sort-object-keys2
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 3.0.8 |
| Published | 2026-03-15 |
| First published | 2018-02-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 57.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Keith Cirkel |
| Maintainers | bluelovers |
| Keywords | arr, array, deep, deterministic, function, js, key, keys, obj, object, order, re, re-order, recursive, recursively, sort, source, stable, util, utils, create-by-yarn-tool, create-by-tsdx |

## Links

- npm: https://www.npmjs.com/package/sort-object-keys2
- Repository: https://github.com/bluelovers/ws-array
- Homepage: https://github.com/bluelovers/ws-array/tree/master/packages/sort-object-keys#readme
- Issues: https://github.com/bluelovers/ws-array/issues
- npm.io page: https://npm.io/package/sort-object-keys2

## Dependencies (1)

- [@lazy-array/util-unique](https://npm.io/package/@lazy-array/util-unique.md) ^1.0.7

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 3.0.8 (latest) — 2026-03-15
- 3.0.7 — 2024-03-09
- 3.0.6 — 2024-03-09
- 3.0.5 — 2022-10-25
- 3.0.4 — 2022-10-14
- 3.0.3 — 2022-10-10
- 3.0.2 — 2022-09-26
- 3.0.1 — 2022-09-26
- 2.0.3 — 2020-05-10
- 2.0.2 — 2020-05-10
- 2.0.1 — 2020-04-05
- 2.0.0 — 2020-04-05
- 1.1.0 — 2019-01-11
- 1.0.3 — 2018-04-18
- 1.0.2 — 2018-04-03
- … 4 more at https://npm.io/package/sort-object-keys2/versions

## README

# sort-object-keys2

對物件的鍵進行排序，支援自訂排序函式、指定鍵順序和原地修改選項。

Sort object keys with custom sort functions, specified key order, and in-place modification options.

## 功能說明 / Features

- **基本排序**: 依字母順序排序物件鍵
- **自訂排序**: 使用自訂排序函式
- **指定鍵順序**: 指定特定鍵的排列順序
- **原地修改**: 可選擇直接修改原物件
- **Basic sorting**: Sort keys alphabetically
- **Custom sorting**: Use custom sort functions
- **Specified key order**: Specify particular key order
- **In-place modification**: Option to modify source object directly

## 安裝 / Install

```bash
yarn add sort-object-keys2
yarn-tool add sort-object-keys2
yt add sort-object-keys2
```

## 使用範例 / Usage Examples

### 基本排序 / Basic Sorting

```typescript
import { sortObjectKeys } from 'sort-object-keys2';

// 依鍵名字母順序排序
const obj = { c: 1, b: 2, a: 3 };
const sorted = sortObjectKeys(obj);

console.log(sorted); // => { a: 3, b: 2, c: 1 }
```

### 指定鍵順序 / Specify Key Order

```typescript
import { sortObjectKeys } from 'sort-object-keys2';

// 指定鍵的優先順序
const obj = { c: 1, b: 2, a: 3 };
const sorted = sortObjectKeys(obj, ['b', 'a', 'c']);

console.log(sorted); // => { b: 2, a: 3, c: 1 }
```

### 自訂排序函式 / Custom Sort Function

```typescript
import { sortObjectKeys } from 'sort-object-keys2';

// 自然排序數字鍵
const obj = { key2: 1, key10: 1, key1: 1 };
const sorted = sortObjectKeys(obj, (a, b) => {
  return a.localeCompare(b, undefined, { numeric: true });
});

console.log(sorted); // => { key1: 1, key2: 1, key10: 1 }
```

### 降序排列 / Descending Order

```typescript
import { sortObjectKeys } from 'sort-object-keys2';

const obj = { a: 1, b: 2, c: 3 };
const sorted = sortObjectKeys(obj, { desc: true });

console.log(sorted); // => { c: 3, b: 2, a: 1 }
```

### 只返回指定鍵 / Return Only Specified Keys

```typescript
import { sortObjectKeys } from 'sort-object-keys2';

const obj = { a: 1, b: 2, c: 3 };
const sorted = sortObjectKeys(obj, { keys: ['b', 'c'], onlyKeys: true });

console.log(sorted); // => { b: 2, c: 3 }
```

### 原地修改 / In-place Modification

```typescript
import { sortObjectKeys } from 'sort-object-keys2';

const obj = { c: 1, b: 2, a: 3 };
sortObjectKeys(obj, { useSource: true });

console.log(obj); // => { a: 3, b: 2, c: 1 }
```

### 允許不存在的鍵 / Allow Non-existent Keys

```typescript
import { sortObjectKeys } from 'sort-object-keys2';

const obj = { a: 1, b: 2 };
const sorted = sortObjectKeys(obj, { keys: ['b', 'c', 'a'], allowNotExists: true });

console.log(sorted); // => { b: 2, c: undefined, a: 1 }
```

## 應用情境 / Application Scenarios

1. **測試穩定性**: 確保物件結構穩定以便比對
2. **API 響應**: 產生一致且可預測的 JSON 響應
3. **表單欄位**: 自訂表單欄位的顯示順序
4. **配置管理**: 排序配置物件以便閱讀

## API 參考 / API Reference

```typescript
declare function sortObject<T>(object: T, options?: sortObject.IOptions & {
    useSource: true;
}): T;
declare function sortObject<T>(object: T, options?: sortObject.IOptions & {
    keys: string[];
    onlyKeys: true;
}): Partial<T>;
declare function sortObject<T>(object: T, options?: sortObject.IOptions): Partial<T>;
declare function sortObject<T>(object: T, sortFn: (a, b) => any): Partial<T>;
declare function sortObject<T>(object: T, sortWith: string[]): Partial<T>;
```

[查看完整 API](index.d.ts)

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