Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
chain-able-deps / src / loop / sort / sortWith.ts
Size: Mime:
import curry from '../../fp/curry'
import slice from '../../native/arraySlice'
import { Comparator } from './typings.sort'

/**
 * Sorts a list according to a list of comparators.
 *
 * @since 5.0.0-beta.5
 * @memberOf sort
 *
 * @param {Array} functions A list of comparator functions.
 * @param {Array} list The list to sort.
 * @return {Array} A new list sorted according to the comarator functions.
 *
 * @func
 * @fork v0.23.0
 * @category Relation
 * @sig [a -> a -> Number] -> [a] -> [a]
 *
 * @example
 *
 *      var alice = {
 *        name: 'alice',
 *        age: 40
 *      };
 *      var bob = {
 *        name: 'bob',
 *        age: 30
 *      };
 *      var clara = {
 *        name: 'clara',
 *        age: 40
 *      };
 *      var people = [clara, bob, alice];
 *      var ageNameSort = R.sortWith([
 *        R.descend(R.prop('age')),
 *        R.ascend(R.prop('name'))
 *      ]);
 *      ageNameSort(people); //=> [alice, clara, bob]
 */
function sortWith<Value = any>(fns: Comparator[], list: Value[]) {
  return slice.call(list, 0).sort((a, b) => {
    let result = 0
    let i = 0
    while (result === 0 && i < fns.length) {
      result = fns[i](a, b)
      i += 1
    }
    return result
  })
}

export default curry(2, sortWith)