-
-
Save bendasvadim/afbb44937c1a539f8fd34f0a8507f213 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Функция сортировки товаров | |
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