Git Product home page Git Product logo

weighted-sum's Introduction

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

weighted-sum's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

weighted-sum's Issues

0 is not a number.

If a row that I'm sorting on is 0, sorting will fail and return an error that says a number is expected.

Example:

const students = [
   {
      id: 'alice',
      mathGrade: 100,
      englishGrade: 75
   },
   {
      id: 'bob',
      mathGrade: 100,
      englishGrade: 0
   }
];

const sortOptions = {
   includeScore: true,
   mathGrade: { weight: 0.7 }},
   englishGrade: {weight: 0.3 }}
};

weightSum(students, sortOptions);

This should sort the students with Alice followed by Bob, but instead it throws an exception.
Weighted sum error: wrong data type for englishGrade. Number is expected

If I change bob's englishGrade to 1, it sorts properly.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.