FrontendDeveloper.in

JavaScript question detail

What is the purpose of compareFunction while sorting arrays

The compareFunction is used to define the sort order. If omitted, the array elements are converted to strings, then sorted according to each character's Unicode code point value.

Let's take an example to see the usage of compareFunction,

let numbers = [1, 2, 5, 3, 4];
numbers.sort((a, b) => b - a);
console.log(numbers); // [5, 4, 3, 2, 1]
Back to all JavaScript questions
Get LinkedIn Premium at Rs 399