I'm working on a JavaScript project where I have an array of objects, and I need to sort it based on multiple criteria. Each object represents a product and has properties like 'price', 'rating', and 'name'. I want to sort the products first by 'price' in ascending order and then, for products with the same price, by 'rating' in descending order.

Here's a simplified example of the array:

const products = [
    { name: 'Product A', price: 25, rating: 4.5 },
    { name: 'Product B', price: 15, rating: 3.8 },
    { name: 'Product C', price: 25, rating: 4.2 },
    { name: 'Product D', price: 20, rating: 4.0 },
];

I want to sort this array to have products sorted first by 'price' in ascending order and then, for products with the same price, by 'rating' in descending order. Could you give a JavaScript code sample illustrating how to accomplish this multi-criteria sorting and describe the main ideas and functions utilized in the code? I tried visiting numerous sites*in search of the answer, but I was unsuccessful. I appreciate your thoughtful ideas.