# sort-paths

> Sort directory paths in an intuitive way.

Latest version **1.1.1** (published 2018-08-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install sort-paths
pnpm add sort-paths
yarn add sort-paths
bun add sort-paths
```

## 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 | 2018-08-18 |
| First published | 2016-09-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 20.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Gergely Hornich |
| Maintainers | ghornich |
| Keywords | sort, order, path, paths |

## Links

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

## Dependencies (1)

- [split-retain](https://npm.io/package/split-retain.md) ^1.0.1

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 1.1.1 (latest) — 2018-08-18
- 1.1.0 — 2016-09-06
- 1.0.0 — 2016-09-03

## README

# sort-paths
Sort directory paths in an intuitive way.

<!-- toc -->

- [Description](#description)
- [Install](#install)
- [Examples](#examples)
- [Documentation](#documentation)
- [TODO](#todo)
- [License](#license)

<!-- tocstop -->

## Description

This module is designed to sort absolute directory paths. Order is determined by:

* **Path depth**  
  Shallower paths come first.
* **Path component types (file or directory)**  
  Files take precedence over directories, e.g. `D:\hello.txt` always comes before `D:\abc\a.jpg`, regardless of alphabetical order.  
* **Lexical comparison**  
  If two path components are of the same type, they are compared lexically, in a case-insensitive fashion.

**Note**: path component type is determined by the trailing separator, e.g.:

| path          | detected types |
| :--           | :--            |
| `/a/b`        | dir, **file**  |
| `/a/b/`       | dir, dir       |
| `D:\cat.jpeg` | dir, file      |
| `D:\music\`   | dir, dir       |

## Install

**Node.js**  
`npm i sort-paths --save`  
`var sortPaths = require('sort-paths');`

**Browser**  
Include either the full or minified version from `/browser`, and use `sortPaths()` globally.  
`<script src="path/to/sort-paths.min.js"></script>`

## Examples

```js
sortPaths(
    [
        '/b/cat.png',
        '/b/dog.jpeg',
        '/a/b/e.txt',
        '/b/',
        '/a/x.txt'
    ],
    '/'
);

/*
    /a/x.txt
    /a/b/e.txt
    /b/
    /b/cat.png
    /b/dog.jpeg
*/
```

—

```js
sortPaths(
    [
        'E:\\',
        'D:\\music\\',
        'D:\\',
        'D:\\music\\song.mp3'
    ],
    '\\'
);

/*
    D:\\
    D:\\music\\
    D:\\music\\song.mp3
    E:\\
*/
```

—

```js
sortPaths(
    [
        { id: 55, path: 'C:/Users/Al/pictures.zip' },
        { id: 2,  path: 'C:/Users/w.dll' },
        { id: 14, path: 'C:/Users/' },
        { id: 30, path: 'C:/Users/Al/' }
    ],
    function (item) { return item.path; },
    '/'
);

/*
    { id: 14, path: 'C:/Users/' }
    { id: 2,  path: 'C:/Users/w.dll' }
    { id: 30, path: 'C:/Users/Al/' }
    { id: 55, path: 'C:/Users/Al/pictures.zip' }
*/
```

## Documentation

`sortPaths(items, [iteratee, ] dirSeparator)`

| name         | type     | description                                                                              |
| :--          | :--      | :--                                                                                      |
| items        | Array<*> | Path strings (or objects) to sort.                                                       |
| [iteratee]   | Function | Optional iteratee, called on each item. Must return a string. Used when sorting objects. |
| dirSeparator | String   | One character.                                                                           |
| &nbsp;       |          |                                                                                          |
| @return      | Array<*> | Sorted items.                                                                            |
| @throws      | Error    | On incorrect argument type/count.                                                        |

## TODO

* Use natural sort for numeric names (e.g. file1, file2, ...), or add a customizable compare function
* Browser tests (e.g. karma), for /browser releases
* Lint

## License

MIT

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