sortWith.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import _curry2 from "./internal/_curry2.js";
  2. /**
  3. * Sorts a list according to a list of comparators.
  4. *
  5. * @func
  6. * @memberOf R
  7. * @since v0.23.0
  8. * @category Relation
  9. * @sig [(a, a) -> Number] -> [a] -> [a]
  10. * @param {Array} functions A list of comparator functions.
  11. * @param {Array} list The list to sort.
  12. * @return {Array} A new list sorted according to the comarator functions.
  13. * @see R.ascend, R.descend
  14. * @example
  15. *
  16. * const alice = {
  17. * name: 'alice',
  18. * age: 40
  19. * };
  20. * const bob = {
  21. * name: 'bob',
  22. * age: 30
  23. * };
  24. * const clara = {
  25. * name: 'clara',
  26. * age: 40
  27. * };
  28. * const people = [clara, bob, alice];
  29. * const ageNameSort = R.sortWith([
  30. * R.descend(R.prop('age')),
  31. * R.ascend(R.prop('name'))
  32. * ]);
  33. * ageNameSort(people); //=> [alice, clara, bob]
  34. */
  35. var sortWith =
  36. /*#__PURE__*/
  37. _curry2(function sortWith(fns, list) {
  38. return Array.prototype.slice.call(list, 0).sort(function (a, b) {
  39. var result = 0;
  40. var i = 0;
  41. while (result === 0 && i < fns.length) {
  42. result = fns[i](a, b);
  43. i += 1;
  44. }
  45. return result;
  46. });
  47. });
  48. export default sortWith;