sort.js 986 B

123456789101112131415161718192021222324252627282930
  1. import _curry2 from "./internal/_curry2.js";
  2. /**
  3. * Returns a copy of the list, sorted according to the comparator function,
  4. * which should accept two values at a time and return a negative number if the
  5. * first value is smaller, a positive number if it's larger, and zero if they
  6. * are equal. Please note that this is a **copy** of the list. It does not
  7. * modify the original.
  8. *
  9. * @func
  10. * @memberOf R
  11. * @since v0.1.0
  12. * @category List
  13. * @sig ((a, a) -> Number) -> [a] -> [a]
  14. * @param {Function} comparator A sorting function :: a -> b -> Int
  15. * @param {Array} list The list to sort
  16. * @return {Array} a new array with its elements sorted by the comparator function.
  17. * @see R.ascend, R.descend
  18. * @example
  19. *
  20. * const diff = function(a, b) { return a - b; };
  21. * R.sort(diff, [4,2,7,5]); //=> [2, 4, 5, 7]
  22. */
  23. var sort =
  24. /*#__PURE__*/
  25. _curry2(function sort(comparator, list) {
  26. return Array.prototype.slice.call(list, 0).sort(comparator);
  27. });
  28. export default sort;