TransWikia.com

Почему не работает сортировка?

Stack Overflow на русском Asked by Ihor Stoner on December 14, 2021

"presents": [{
    "id": 61,
    "name": "Дорожный Набор Для Бритья Edwin Jagger Ejlt2M3",
    "price": "4188",
    "brand": "Edwin Jagger",
    "img": "https://barbercompany.com/image/cache/webp/catalog/product/2015-11-294-543x543.webp"
  },
  {
    "id": 62,
    "name": "Подарочный набор для бритья Taylor of Old Bond Street Shaving Brush, Mach 3 Razor & Jermyn Street Collection Shaving Cream 150 г",
    "price": "3788",
    "brand": "Old Bond",
    "img": "https://barbercompany.com/image/cache/webp/catalog/product-2018/05-2018/Taylor-of-Old-Bond-Street-Shaving-Brush-Mach-3-Razor-Jermyn-Street-Collection-Shaving-Cream-543x543.webp"
  },
  {
    "id": 63,
    "name": "Масло Для Бороды Proraso Hot Oil Beard Treatment 4 х 17 мл",
    "price": "298",
    "brand": "Proraso",
    "img": "https://barbercompany.com/image/cache/webp/catalog/Proraso/maslo-dlja-borodi-proraso-hot-oil-beard-treatment-4-h-17-ml-543x543.webp"
  },
  {
    "id": 64,
    "name": "Маникюрный Набор Унисекс на 6 Предметов Truefitt & Hill Large Manicure Set 6 Piece Tan",
    "price": "4788",
    "brand": "Truefitt & Hill",
    "img": "https://barbercompany.com/image/cache/webp/catalog/product/2017-03-truefitt-hill-large-manicure-set-6-piece-tan-543x543.webp"
  },
  {
    "id": 65,
    "name": "Подарочный Набор Muhle Organic Body Care Set",
    "price": "948",
    "brand": "Muhle",
    "img": "https://barbercompany.com/image/cache/webp/catalog/product-2019/12-2019/Muhle_Organic_Body_Care_Set-543x543.webp"
  }
]

Почему не сортирует по цене data.sort()?

const radioCheckArr = document.querySelectorAll('.choose-master__radio');
radioCheckArr.forEach((radio) => {
  if (radio.checked && radio.id === 'filter-0') {
    if (dataCategory.length === 0) {
      //exp to cheap
      data.sort((product1, product2) => product1.price > product2.price ? 1 : -1);
      filterData.reverse();
      createCards(data, parent, category);
    } else {
      dataCategory.sort((product1, product2) => product1.price > product2.price ? 1 : -1);
      dataCategory.reverse();
      createCards(dataCategory, parent, category);
    }

  } else if (radio.checked && radio.id === 'filter-1') {
    //cheap to exp
    if (dataCategory.length === 0) {
      data.sort((product1, product2) => product1.price > product2.price ? 1 : -1);
      createCards(data, parent, category);
    } else {
      dataCategory.sort((product1, product2) => product1.price > product2.price ? 1 : -1);
      createCards(dataCategory, parent, category);
    }
  }
})

One Answer

data.sort((product1, product2) => product1.price - product2.price);

Answered by Igor on December 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP