Сортировка в алфавитном порядке идеальна. Это сортировка ценового массива, но не печать объектов по отсортированной цене. В console.log все работает идеально, но когда я выбираю вариант цены от низкой к высокой, он не вносит никаких изменений в элементы, хотя это происходит в консоли.
Код: Выделить всё
$(document.body).on('change', '#products-sorting', function() {
var priceArr = [];
var nameArr = [];
for (var i = 0; i < tempArr.length; i++) {
var price = $(tempArr[i]).find('.addToWishlist').data("price").replace("$", "");
var name = $(tempArr[i]).find('.addToWishlist').data("name");
priceArr.push(price)
nameArr.push(name);
}
//sort by Price
for (var n = 0; n < priceArr.length; n++) {
for (var i = 0; i < tempArr.length; i++) {
//sort price low to high
if ($(this).val() === 'Price, low to high') {
priceArr.sort(function(a, b) {
return a - b
})
}
//sort price high to low
if ($(this).val() === 'Price, high to low') {
priceArr.sort(function(a, b) {
return b - a
})
}
var price = $(tempArr[i]).find('.addToWishlist').data("price").replace("$", "");
if (priceArr[n] == price)
$("#product-items").append(tempArr[i])
}
}
//console.log(priceArr)
//sort Alphabetically
for (var n = 0; n < nameArr.length; n++) {
for (var i = 0; i < tempArr.length; i++) {
//sort alphabetically A to Z
if ($(this).val() === 'Alphabetically, A to Z')
nameArr.sort();
//sort alphabetically Z to A
if ($(this).val() === 'Alphabetically, Z to A') {
nameArr.sort();
nameArr.reverse()
}
var name = $(tempArr[i]).find('.addToWishlist').data("name");
if (nameArr[n] == name)
$("#product-items").append(tempArr[i])
}
}
})Код: Выделить всё
Sort by
Featured
Alphabetically, A to Z
Alphabetically, Z to A
Price, low to high
Price, high to low
Подробнее здесь: https://stackoverflow.com/questions/691 ... -in-jquery