Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bendasvadim/afbb44937c1a539f8fd34f0a8507f213 to your computer and use it in GitHub Desktop.
Save bendasvadim/afbb44937c1a539f8fd34f0a8507f213 to your computer and use it in GitHub Desktop.
// Функция сортировки товаров
function sortCatalog(products, section, reverse = false) {
// Переменные
var arProductsList = [];
var strHtml = '';
// Переделываем в массив
products.each(function (e) {
arProductsList.push(products[e]);
});
// Проверяем как сортировать массив и сортируем его
if (reverse) {
arProductsList.sort(comparePrice).reverse();
} else {
arProductsList.sort(comparePrice);
}
// Очищаем каталог
section.html('');
// Добовляем элементы на страницу
for(var i = 0; i < arProductsList.length; i++) {
$('.catalog_list').append(arProductsList[i]);
}
}
// Функция учавствующая в сортировке
function comparePrice(prodictA, productB) {
return prodictA.getAttribute('data-sort-price') - productB.getAttribute('data-sort-price');
}
// Использование
sortCatalog($('.catalog_item.item_p'), $('.catalog_list'), false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment