简体   繁体   English

如何按数组对项目进行排序?

[英]How do I sort items by array?

I understand that the wording of the question is incorrect (if someone can write it correctly, please).我知道问题的措辞不正确(如果有人可以正确写出,请)。 The task is this, I have 30 elements on the page and I need to sort them with the resulting array.任务是这样的,我在页面上有 30 个元素,我需要用结果数组对它们进行排序。 That is, I get an array - let order = [2, 5, 3, 6, 12 ...] and sorting should take place in accordance with this order, that is, the first element is the 2nd element from HTML, the second element is the 5th element from HTML (according to the given array).也就是说,我得到一个数组 - let order = [2, 5, 3, 6, 12 ...]并且排序应该按照这个顺序进行,也就是说,第一个元素是来自 HTML 的第二个元素,第二个元素是 HTML 中的第 5 个元素(根据给定的数组)。 The initial order is equal to the number in data-custom-sort .初始顺序等于data-custom-sort

There will be many such an array.这样的数组会有很多。 And I don't understand how to do it universally.我不明白如何普遍地做到这一点。 Can someone have any ideas?有人可以有任何想法吗?

I have not formulated very well, so if you have questions - ask.我没有很好地制定,所以如果你有问题 - 问。

The HTML is something like this: HTML是这样的:

<a id="sort-best" class="choose-cat">best</a>

<div>
    <article data-custom-sort="1">
        ...
    </article>
    <article data-custom-sort="2">
        ...
    </article>
    <article data-custom-sort="3">
        ...
    </article>
    //and etc

</div>

These are product cards in the catalog.这些是目录中的产品卡。 I need to sort them我需要对它们进行排序

document.querySelector('#sort-best').onclick = sortBest;

function sortBest() {
  let nav = document.querySelector('#game-items-cart');
  for (let i = 0; i < nav.children.length; i++) {
    for (let j = i; j < nav.children.length; j++) {
      if (+nav.children[i].getAttribute('data-sort') > +nav.children[j].getAttribute('data-sort')) {
        replaceNode = nav.replaceChild(nav.children[j], nav.children[i]);
        insertAfter(replaceNode, nav.children[i]);
      }
    }
  }
}

function insertAfter(elem, refElem) {
  return refElem.parentNode.insertBefore(elem, refElem.nextSibling);
}

I used this code to sort through the data attributes.我使用此代码对data属性进行排序。 That is, the number in the data attribute = the ordinal after sorting .数据属性中的数字=排序后的序数

Like this?像这样?

 let order = [2, 1, 3]; const container = document.getElementById("container"); document.getElementById("sort-best").addEventListener("click", e => { e.preventDefault() order.forEach(idx => container.appendChild(container.querySelector("[data-custom-sort='" + idx + "']"))) })
 <a id="sort-best" class="choose-cat">best</a> <div id="container"> <article data-custom-sort="1"> One </article> <article data-custom-sort="2"> Two </article> <article data-custom-sort="3"> Three </article> </div>

More generic:更通用:

 const sortArticles = (cont, order) => { const container = document.getElementById(cont); order.forEach(idx => container.appendChild(container.querySelector("[data-custom-sort='" + idx + "']"))) }; document.getElementById("sort").addEventListener("click", e => { const tgt = e.target; if (tgt.classList.contains("choose-cat")) { e.preventDefault() sortArticles("container", tgt.dataset.order.split(",")) } })
 <div id="sort"> <a id="sort-best" class="choose-cat" data-order="3,1,2">best</a> | <a id="sort-default" class="choose-cat" data-order="1,2,3">default</a> </div> <div id="container"> <article data-custom-sort="1"> One </article> <article data-custom-sort="2"> Two </article> <article data-custom-sort="3"> Three </article> </div>

Here is another way of doing this这是另一种方法

 // Sort reference array const sortRef = [2, 5, 3, 1, 4]; // Get, sort, update function const sortFn = () => { // Apply new order by sorting and replacing sortContainer content const newArray = []; for(let i = 0; i < sortRef.length; i++) newArray.push(document.querySelector("[data-custom-sort='" + sortRef[i] + "']").outerHTML); // Update html document.getElementById("sortContainer").innerHTML = newArray.join(''); } // Add click event document.getElementById("clickMe").addEventListener('click', event => { sortFn(); });
 article { border: 1px solid #ff0000; padding: 3px; width: 100px; }
 <div id="sortContainer"> <article data-custom-sort="1"> 1 </article> <article data-custom-sort="2"> 2 </article> <article data-custom-sort="3"> 3 </article> <article data-custom-sort="4"> 4 </article> <article data-custom-sort="5"> 5 </article> </div> <p></p> <button id="clickMe">Sort html data</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM