https://github.com/dcamilleri/weighted-sum
Simplified weighted sum algorithm implementation used on Mealou (https://m.me/Mealoubot | http://www.mealou.co)
https://github.com/dcamilleri/weighted-sum
algorithm sum weighted
Last synced: 16 days ago
JSON representation
Simplified weighted sum algorithm implementation used on Mealou (https://m.me/Mealoubot | http://www.mealou.co)
- Host: GitHub
- URL: https://github.com/dcamilleri/weighted-sum
- Owner: dcamilleri
- License: mit
- Created: 2016-06-11T17:49:51.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-07-13T17:55:39.000Z (over 8 years ago)
- Last Synced: 2025-10-29T22:51:56.714Z (4 months ago)
- Topics: algorithm, sum, weighted
- Language: JavaScript
- Homepage:
- Size: 9.77 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# weighted-sum
Weighted sum algorithm implementation used on Mealou (https://m.me/Mealoubot | https://www.mealou.co)
Algorithm Documentation:
- EN: [Wikipedia](https://en.wikipedia.org/wiki/Weighted_sum_model)
- FR: [Wikipedia](https://fr.wikipedia.org/wiki/M%C3%A9thode_math%C3%A9matique_d%27analyse_multicrit%C3%A8re)
## Install
[](https://www.npmjs.com/package/weighted-sum)
[](https://travis-ci.com/dcamilleri/weighted-sum)
```
$ npm install weighted-sum
```
## Usage
Sorting three restaurants by `note` and `distance` fields:
```js
const weightSum = require('weighted-sum')
const data = [
{
id: 'restaurant 1',
note: 4,
distance: 626
},
{
id: 'restaurant 2',
note: 4,
distance: 410
},
{
id: 'restaurant 3',
note: 3,
distance: 700
}
]
const sortOptions = {
includeScore: false,
note: {
weight: 0.4,
sort: 'asc'
},
distance: {
weight: 0.6,
sort: 'desc'
}
}
// Output: ['restaurant 2', restaurant 1', restaurant 3]
const sortedItems = weightSum(data, sortOptions)
```
## API
### weightSum(data, sortOptions)
#### data
Type: `Array`
- id field is **mandatory**
- Sorting criterias must be numbers
#### sortedOptions
Type: `Object`
- `includeScore`: whether or not include sorting score in result. Default is `false`
- `weight` is **mandatory**
- sum of all `weight` must equal 1
- Default `sort` is `asc`
## License
MIT © [Dorian Camilleri](https://github.com/dcamilleri>)