Skip to content

dcamilleri/weighted-sum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

weighted-sum

Weighted sum algorithm implementation used on Mealou (https://m.me/Mealoubot | https://www.mealou.co)

Algorithm Documentation:

Install

NPM

Build Status

$ npm install weighted-sum

Usage

Sorting three restaurants by note and distance fields:

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

About

Simplified weighted sum algorithm implementation used on Mealou (https://m.me/Mealoubot | http://www.mealou.co)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published