JavaScript & V8 Internals (536 Qs)•Part 4: Prototypes, Object Methods & Event Loop (174 - 253)
What is the purpose of compareFunction while sorting arrays
junior LevelMost AskedCode ExampleEvent LoopPrototypesAlgorithms
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,
javascript
let numbers = [1, 2, 5, 3, 4];
numbers.sort((a, b) => b - a);
console.log(numbers); // [5, 4, 3, 2, 1]