I
I
Igor Myasnikov2018-02-06 18:37:35
JavaScript
Igor Myasnikov, 2018-02-06 18:37:35

How to sort the elements of a list?

Good day.
There is a code:

const breachPreviewList = document.querySelector('.breach-preview-list');
breaches.forEach((breach) => {
        let li = document.createElement('li'),
            p1 = document.createElement('p'),
            p2 = document.createElement('p'),
                ...

    p1.innerHTML = `${breach.option1}`; 
    p2.innerHTML = `${breach.option2}`;
    ....
    li.dataset.sort = `${breach.tierNum}`; 


    li.appendChild(p1);
    li.appendChild(p2);
    ...
    breachPreviewList.appendChild(li);
    });

I get the data to fill in via fetch.
How can I sort only created lis for each breach by the dataset.sort attribute?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anatoly Zharov, 2018-02-06
@SeaBreeze876

In fact, it's easier to sort breaches first, and then create a list from it

breaches
    .sort((a, b) => a.tierNum - b.tierNum)
    .forEach(...)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question