Sobes.tech
Junior

How would you sort arrays of numbers?

sobes.tech AI

Answer from AI

Built-in sort() method with a comparison function.

const numbers = [3, 1, 4, 1, 5, 9, 2, 6];

// Comparison function for sorting in ascending order
numbers.sort((a, b) => a - b);

// Comparison function for sorting in descending order
// numbers.sort((a, b) => b - a);

// numbers is now sorted