Published on January 26, 2024By DeveloperBreeze

// Array of products with id, name, and price
const products = [
    { id: 3, name: 'Laptop', price: 1200 },
    { id: 1, name: 'Smartphone', price: 500 },
    { id: 2, name: 'Tablet', price: 300 }
];

// Sort the products array by price in ascending order
const sortedProducts = products.sort((a, b) => a.price - b.price);

// Display the sorted products
console.log('Sorted Products:', sortedProducts);

Comments

Please log in to leave a comment.

Continue Reading:

Non-Primitive Data Types (Objects, Arrays, and Functions)

Published on December 11, 2024

javascript