Last active
October 20, 2020 17:41
-
-
Save pniaps/96c235cefa61e643e246efd96a1235fc to your computer and use it in GitHub Desktop.
Ordenar ofertas de pccomponentes por descuento
This file contains 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
//https://stackoverflow.com/questions/282670/easiest-way-to-sort-dom-nodes | |
var list = document.querySelectorAll('.row.page-0'); | |
var items = list[0].childNodes; | |
var itemsArr = []; | |
for (var i in items) { | |
if (items[i].nodeType == 1) { // get rid of the whitespace text nodes | |
itemsArr.push(items[i]); | |
} | |
} | |
itemsArr.sort(function (a, b) { | |
if (!a.querySelectorAll('.c-product-card__discount').length && !b.querySelectorAll('.c-product-card__discount').length) { | |
return 0; | |
} | |
if (!a.querySelectorAll('.c-product-card__discount').length) { | |
return 1; | |
} | |
if (!b.querySelectorAll('.c-product-card__discount').length) { | |
return -1; | |
} | |
var adto = parseFloat(a.querySelectorAll('.cy-product-discount-ammount')[0].innerHTML); | |
var bdto = parseFloat(b.querySelectorAll('.cy-product-discount-ammount')[0].innerHTML); | |
return adto == bdto ? 0 : (adto > bdto ? 1 : -1); | |
}); | |
for (i = 0; i < itemsArr.length; ++i) { | |
list[0].appendChild(itemsArr[i]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment