mdn / ai-feedback

Feedback about AI Help
Other
8 stars 5 forks source link

[AI Help] Question: what is the difference to toSorted()? #32

Open tscherter opened 10 months ago

tscherter commented 10 months ago

What is the issue with the answer?

The AI is telling me: "The toSorted() method has the same algorithm as Array.prototype.toSorted(), except that it sorts the values numerically instead of as strings by default."

But [21, 12, 3, 1].sort() >  [1, 12, 21, 3] AND [21, 12, 3, 1].toSorted() >  [1, 12, 21, 3] are sorted by their string values.

How would you categorize this issue?

What did you ask?

  1. How can I sort an Array in JavaScript?
  2. what is the difference to toSorted()?

What did AI Help answer?

In JavaScript, you can sort an array using the sort() method. This method sorts the elements of an array in place and returns the sorted array. You can also provide a custom comparison function to define the sort order.

The toSorted() method is specific to typed arrays in JavaScript. It is the copying version of the sort() method for typed array instances. It returns a new typed array with the elements sorted in ascending order. The toSorted() method has the same algorithm as Array.prototype.toSorted(), except that it sorts the values numerically instead of as strings by default.

You can find more information about sorting arrays in JavaScript on MDN's documentation for the sort() method and the toSorted() method.

What MDN pages did AI Help consult?

caugner commented 7 months ago

Hi @tscherter, thank you for flagging this answer. 👋

The cause here seems to be that although you're asking about Array, not TypedArray, the consulted MDN content contains both TypedArray.prototype.toSorted() and Array.prototype.toSorted(), and the LLM (GPT-3.5, assuming you're on the free tier) mixes up the content of the two articles (which we give both as context).

Note that the TypedArray.prototype.toSorted() article indeed says:

The toSorted() method [...] has the same algorithm as Array.prototype.toSorted(), except that it sorts the values numerically instead of as strings by default.

So the second paragraph consistently refers to "typed arrays" (aka TypedArray) and mentions the Array.prototype.toSorted() method indirectly, correctly implying that it is sorting values as strings, whereas "The toSorted() method [of typed arrays] (...) sorts values numerically".

The good news is that GPT-4 gets it right:

Sorting an array in JavaScript can be achieved using the Array.prototype.sort() method. This method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings and comparing their sequences of UTF-16 code units values. However, you can provide a compareFunction to sort() if you need custom sorting logic, such as numerical sorting.

Here's a basic example of using sort():

const fruits = ['banana', 'apple', 'orange'];
fruits.sort();
console.log(fruits); // Output: ['apple', 'banana', 'orange']

And with a compareFunction for numbers:

const numbers = [1, 10, 21, 2];
numbers.sort((a, b) => a - b);
console.log(numbers); // Output: [1, 2, 10, 21]

For more details, refer to the MDN documentation on Array.prototype.sort().

Difference to toSorted()

The Array.prototype.toSorted() method, introduced in newer versions of JavaScript, provides a way to sort an array without mutating the original array. Instead, it returns a new array with the sorted elements. This is useful when you want to keep the original array unchanged.

Here's an example of using toSorted():

const numbers = [1, 10, 21, 2];
const sortedNumbers = numbers.toSorted((a, b) => a - b);
console.log(sortedNumbers); // Output: [1, 2, 10, 21]
console.log(numbers); // Output: [1, 10, 21, 2], original array remains unchanged

The key difference between sort() and toSorted() is that sort() sorts the array in place, modifying the original array, whereas toSorted() returns a new array with sorted elements, leaving the original array unchanged.

For more details on toSorted(), you can refer to the MDN documentation on Array.prototype.toSorted().