Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/doesdev/leven-sort
Simply sort by similarity, starring Levenshtein via leven
https://github.com/doesdev/leven-sort
Last synced: about 20 hours ago
JSON representation
Simply sort by similarity, starring Levenshtein via leven
- Host: GitHub
- URL: https://github.com/doesdev/leven-sort
- Owner: doesdev
- License: mit
- Created: 2016-04-12T22:02:40.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-08-20T03:02:51.000Z (over 2 years ago)
- Last Synced: 2024-10-28T13:34:01.228Z (22 days ago)
- Language: JavaScript
- Size: 83 KB
- Stars: 9
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# leven-sort [![npm version](https://badge.fury.io/js/leven-sort.svg)](http://badge.fury.io/js/leven-sort) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard) [![Dependency Status](https://dependencyci.com/github/doesdev/leven-sort/badge)](https://dependencyci.com/github/doesdev/leven-sort)
Simply sort by similarity, starring Levenshtein via [leven](https://github.com/sindresorhus/leven).
Allows sorting of an array of strings or array of objects by similarity. For arrays of objects
you can sort by up to two keys.## install
`npm i leven-sort --save`## api
**leven-sort** only exports one function, but it has 3 distinct signatures
1. Sort simple text array
`arrayToSort`: Array to sort (sorted in place)
`sourceText`: The text to check similarity of2. Sort array of objects, with optional secondary sort
`arrayToSort`: Array to sort (sorted in place)
`sourceText1`: The text to check similarity of
`key1`: Property name to be compared in each object
`sourceText2` *(optional)*: If performing a secondary sort, this is the text to check key2 by
`key2` *(optional)*: For secondary sort on object, this is the second property to check3. Sort array of objects by multiple keys, each by the same source and with equal weight
`arrayToSort`: Array to sort (sorted in place)
`sourceText`: The text to check similarity of
`arrayOfKeys`: Array of property names to compare## usage
Sort simple text array
```javascript
const levenSort = require('leven-sort')
const sourceName = 'Bill Griffin'
const nameAry = [
'Carl Martinez',
'Roger Davis',
'William George',
'Andrew Torres',
'Billy Campbell',
'Alan King',
'Benjamin Wilson',
'Bill Griffin',
'Dennis Smith',
'Billy Griffith'
]
let levSorted = levenSort(nameAry, sourceName)
```Sort array of objects, with secondary sort
```javascript
const levenSort = require('leven-sort')
const sourceFirst = 'Bill'
const sourceLast = 'Griffin'
const nameObjAry = [
{ first: 'Carl', last: 'Martinez' },
{ first: 'Roger', last: 'Davis' },
{ first: 'William', last: 'George' },
{ first: 'Andrew', last: 'Torres' },
{ first: 'Billy', last: 'Campbell' },
{ first: 'Alan', last: 'King' },
{ first: 'Benjamin', last: 'Wilson' },
{ first: 'Bill', last: 'Griffin' },
{ first: 'Dennis', last: 'Smith' },
{ first: 'Billy', last: 'Griffith' }
]let levSorted = levenSort(nameObjAry, sourceFirst, 'first', sourceLast, 'last')
```
Sort array of objects by multiple keys
```javascript
const levenSort = require('leven-sort')
const source = 'Bill'
const nameObjAry = [
{ first: 'Carl', last: 'Martinez' },
{ first: 'Roger', last: 'Davis' },
{ first: 'William', last: 'George' },
{ first: 'Andrew', last: 'Torres' },
{ first: 'Billy', last: 'Campbell' },
{ first: 'Alan', last: 'King' },
{ first: 'Benjamin', last: 'Wilson' },
{ first: 'Bill', last: 'Griffin' },
{ first: 'Dennis', last: 'Smith' },
{ first: 'Billy', last: 'Griffith' }
]let levSorted = levenSort(nameObjAry, source, ['first', 'last'])
```