path-sort2
Version:
Sort a list of file/directory paths
58 lines (45 loc) • 901 B
Markdown
Sort a list of file/directory paths, such that something like this:
```js
const pathSort = require('path-sort2');
let files = [
'a/c',
'a/a/a',
'b/b',
'b/a/2/a',
'b/a/1/b',
'a/b',
'b/a/1/a',
'a/a/b',
'b/a/2/b',
'c',
];
let sortedFiles = pathSort(files);
// [
// 'a/b',
// 'a/c',
// 'a/a/a',
// 'a/a/b',
// 'b/b',
// 'b/a/1/a',
// 'b/a/1/b',
// 'b/a/2/a',
// 'b/a/2/b',
// 'c',
// ]
```
```sh
npm install path-sort2
```
```js
const pathSort = require('path-sort2');
pathSort(files); // sorted files
pathSort(files, '/'); // custom seperator (defaults to `path.sep`)
files.sort(pathSort.standalone()); // sorted files
files.sort(pathSort.standalone('/')); // custom seperator (defaults to `path.sep`)
```
> **Note:** Using `pathSort.standalone()` is a little bit slower, but is
> sometimes more convenient.