Created
June 28, 2017 10:25
-
-
Save 4yvi/6eb2dd68c3b0b3e2d12b156a7eed556b 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