Skip to content

Commit

Permalink
Enhance compare function docs (#32310)
Browse files Browse the repository at this point in the history
* Enhance compare function docs

* Some rewrites

---------

Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
  • Loading branch information
IAfanasov and Josh-Cena authored Mar 15, 2024
1 parent 3ec1741 commit e46c58e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ sort(compareFn)

- `compareFn` {{optional_inline}}

- : A function that defines the sort order. The return value should be a number whose sign indicates the relative order of the two elements: negative if `a` is less than `b`, positive if `a` is greater than `b`, and zero if they are equal. `NaN` is treated as `0`. The function is called with the following arguments:
- : A function that determines the order of the elements. The function is called with the following arguments:

- `a`
- : The first element for comparison. Will never be `undefined`.
- `b`
- : The second element for comparison. Will never be `undefined`.

It should return a number where:

- A negative value indicates that `a` should come before `b`.
- A positive value indicates that `a` should come after `b`.
- Zero or `NaN` indicates that `a` and `b` are considered equal.

To memorize this, remember that `(a, b) => a - b` sorts numbers in ascending order.

If omitted, the array elements are converted to strings, then sorted according to each character's Unicode code point value.

### Return value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ toSorted(compareFn)
### Parameters

- `compareFn` {{optional_inline}}

- : Specifies a function that defines the sort order. If omitted, the array elements are converted to strings, then sorted according to each character's Unicode code point value.

- `a`
- : The first element for comparison.
- `b`
- : The second element for comparison.
- : A function that determines the order of the elements. If omitted, the array elements are converted to strings, then sorted according to each character's Unicode code point value. See {{jsxref("Array/sort", "sort()")}} for more information.

### Return value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@ sort(compareFn)

- `compareFn` {{optional_inline}}

- : A function that defines the sort order. The return value should be a number whose sign indicates the relative order of the two elements: negative if `a` is less than `b`, positive if `a` is greater than `b`, and zero if they are equal. `NaN` is treated as `0`. The function is called with the following arguments:
- : A function that determines the order of the elements. The function is called with the following arguments:

- `a`
- : The first element for comparison. Will never be `undefined`.
- : The first element for comparison.
- `b`
- : The second element for comparison. Will never be `undefined`.
- : The second element for comparison.

It should return a number where:

- A negative value indicates that `a` should come before `b`.
- A positive value indicates that `a` should come after `b`.
- Zero or `NaN` indicates that `a` and `b` are considered equal.

To memorize this, remember that `(a, b) => a - b` sorts numbers in ascending order.

If omitted, the typed array elements are sorted according to numeric value.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ toSorted(compareFn)

- `compareFn` {{optional_inline}}

- : Specifies a function that defines the sort order. If omitted, the typed array elements are sorted according to numeric value.

- `a`
- : The first element for comparison.
- `b`
- : The second element for comparison.
- : A function that determines the order of the elements. If omitted, the typed array elements are sorted according to numeric value. See {{jsxref("TypedArray/sort", "sort()")}} for more information.

### Return value

Expand Down

0 comments on commit e46c58e

Please sign in to comment.