weighted-sum
Weighted sum algorithm implementation used on Mealou (https://m.me/Mealoubot | https://www.mealou.co)
Algorithm Documentation:
Install
$ npm install weighted-sum
Usage
Sorting three items by note
and distance
fields:
const weightSum = const data = id: 'item 1' note: 4 distance: 626 id: 'item 2' note: 4 distance: 410 id: 'item 3' note: 3 distance: 700 const sortOptions = includeScore: true note: weight: 04 sort: 'asc' distance: weight: 06 sort: 'desc' // Output: ['item 2', item 1', item 3]const sortedItems =
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 isfalse
weight
is mandatory- sum of all
weight
must equal 1 - Default
sort
isasc
License
MIT © Dorian Camilleri