0

How to sort products created in the "Product" class and recorded in the "Shop" class at a price in order of growth.The essence of the method consists in sorting by price (with the ability to set the order) sortByPrice (order)

Call example:

console.log (shop.sortProductsByPrice (Product.SORT_ORDER_ASC));

//Product Creation Class
class Product {
    constructor(name, count, price) {
        this.name = name;
        this.count = count;
        this.price = price;
    }
}
//Сlass where products are recorded
class Shop {
    constructor(products) {
        this.products = [];
    }

    //method for adding a product
    addProduct(newProduct) {
        this.products.push(newProduct);
    }
    //method for sorting the product at its price
    sortProductsByPrice(dataset, order, prop) {
        let sorted = dataset.sort((a, b) => {
            let innerA;
            for (let subObjectName in a) {
                let inner = a[subObjectName];
                innerA = inner;
                break;
            }
            let innerB;
            for (let subObjectName in b) {
                let inner = b[subObjectName];
                innerB = inner;
                break;
            }
            return order(innerA[prop], innerB[prop])
        });
        return sorted;
    }
}

const shop = new Shop();
// create products
shop.addProduct(new Product("product 1", 1, 2000));
shop.addProduct(new Product("product 2", 1, 700));
shop.addProduct(new Product("product 3", 2, 800));
shop.addProduct(new Product("product 4", 3, 1000));
//sort by the specified key "price"
let res = shop.sortProductsByPrice(shop.products,
    (a, b) => a - b, "price");
console.log(res);

1 Answer 1

1

I removed the for... in iteration. I don't think it's doing what you mean for it to be doing.

//Product Creation Class
class Product {
    constructor(name, count, price) {
        this.name = name;
        this.count = count;
        this.price = price;
    }
}
//Сlass where products are recorded
class Shop {
    constructor(products) {
        this.products = [];
    }

    //method for adding a product
    addProduct(newProduct) {
        this.products.push(newProduct);
    }
    //method for sorting the product at its price
    sortProductsByPrice(dataset, order, prop) {
        let sorted = dataset.sort((a, b) => {
            
            return order(a[prop], b[prop])
        });
        return sorted;
    }
}

const shop = new Shop();
// create products
shop.addProduct(new Product("product 1", 1, 2000));
shop.addProduct(new Product("product 2", 1, 700));
shop.addProduct(new Product("product 3", 2, 800));
shop.addProduct(new Product("product 4", 3, 1000));
//sort by the specified key "price"
let res = shop.sortProductsByPrice(shop.products,
    (a, b) => a - b, "price");
console.log(res);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.