Skip to content

Incorrect JSDoc description for Array#sort default algorithm "ASCII character order" (actually UTF-16 code unit order) #60978

@lionel-rowe

Description

@lionel-rowe

⚙ Compilation target

esnext

⚙ Library

5.7.2

Missing / Incorrect Definition

       * @param compareFn Function used to determine the order of the elements. It is expected to return
       * a negative value if the first argument is less than the second argument, zero if they're equal, and a positive
-      * value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
+      * value otherwise. If omitted, the elements are sorted in ascending, UTF-16 code unit order.

Sample Code

// N/A (issue relates to JSDoc/intellisense)

Documentation Link

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

The sort() method of Array instances sorts the elements of an array in place and returns the reference to the same array, now sorted. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code unit values.

https://tc39.es/ecma262/multipage/indexed-collections.html#sec-comparearrayelements

  1. Let xSmaller be ! IsLessThan(xString, yString, true).
  2. If xSmaller is true, return -1𝔽.
    ? 9. Let ySmaller be ! IsLessThan(yString, xString, true).
  3. If ySmaller is true, return 1𝔽.

https://tc39.es/ecma262/multipage/abstract-operations.html#sec-islessthan

  1. If px is a String and py is a String, then
    a. Let lx be the length of px.
    b. Let ly be the length of py.
    c. For each integer i such that 0 ≤ i < min(lx, ly), in ascending order, do
    i. Let cx be the numeric value of the code unit at index i within px.
    ii. Let cy be the numeric value of the code unit at index i within py.
    iii. If cx < cy, return true.
    iv. If cx > cy, return false.
    d. If lx < ly, return true. Otherwise, return false.

(Emphasis added)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Help WantedYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some cases

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions