DeveloperBreeze

Sort By Property Development Tutorials, Guides & Insights

Unlock 1+ expert-curated sort by property tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your sort by property skills on DeveloperBreeze.

JavaScript Sort Array of Objects by Property

Code January 26, 2024
javascript

// 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);