Sort() Method in JavaScript 🔥 #javascript #DSA #javascriptinterview

preview_player
Показать описание
In JavaScript, the sort() method is used to arrange the elements of an array in ascending order by default. The basic syntax is:

However, the sort() method can also take a compare function as an argument to define a custom sorting order. The compare function should return a negative, zero, or positive value, depending on the desired sorting order.

Here's an example of using sort() with a compare function to sort numbers in descending order:

let numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
return b - a;
});

// Output: [9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1]

In this example, the sort() method is used with a compare function that subtracts b from a, resulting in a descending order.
Рекомендации по теме
Комментарии
Автор

In your Book how many questions you have solved in that

indian-shorts