multi-compare
Compose array sort compare functions
Last updated 5 years ago by parshap .
WTFPL · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install multi-compare 
SYNC missed versions from official npm registry.

multi-compare

Compose multiple array sort compare functions into one. Given compare functions are used in order until one of them returns a non-0 value.

Example

var createMultiCompare = require("multi-compare");

var data = [
  {
    name: "Parsha",
    age: 29,
    weight: 200,
  },
  {
    name: "Shayna",
    age: 25,
    weight: 130,
  },
  {
    name: "Alyssa",
    age: 25,
    weight: 140,
  },
];

data.sort(createMultiCompare([
  // First compare by age
  function compareAge(a, b) {
    return a.age - b.age;
  },
  // Break ties by comparing weight
  function compareWeight(a, b) {
    return a.weight - b.weight;
  },
]));

console.log(data)
// ->
// Shayna
// Alyssa
// Parsha

API

var createMultiCompare = require("multi-compare");

var compareFn = createMultiCompare(compareFns)

Compose given compare functions into one. Given compare functions are used in order until one of them returns a non-0 value.

Install

npm: multi-compare

npm install multi-compare

Current Tags

  • 1.0.0                                ...           latest (5 years ago)

1 Versions

  • 1.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (1)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |