array/sort.js

  1. import { convertArrayMethod } from './convertArrayMethod'
  2. /**
  3. * Replaces by a sorted array, in natural order or according to the optional <code>comparator</code>.
  4. * @function
  5. * @memberof array
  6. * @param {Object} object The object to modify.
  7. * @param {Array|string} path The path of the property to set.
  8. * @param {function?} comparator The comparator function.
  9. * @return {Object} Returns the updated object.
  10. * @example sort({ nested: { prop: [2, 4, 3, 1] } }, 'nested.prop') // => { nested: { prop: [1, 2, 3, 4] } }
  11. * @example sort({ nested: { prop: [2, 4, 3, 1] } }, 'nested.prop', (a, b) => b - a) // => { nested: { prop: [4, 3, 2, 1] } }
  12. * @see {@link https://mdn.io/Array.prototype.sort|Array.prototype.sort} for more information.
  13. * @since 1.0.0
  14. */
  15. const sort = convertArrayMethod('sort')
  16. export { sort }