How to display sorted values?

  • 0
    Problem with displaying sorted emails. I don't understand how the sequence can be changed.

    <p class="numb" data-price="450">450</p>
    <p class="numb" data-price="730">730</p>
    <p class="numb" data-price="121">121</p>

    let num = $("[data-price]");
        let arrayNum = [];
        num.each(function () {
            arrayNum.push($(this).data("price"))
        });
        arrayNum.sort(function (a,b) {
            return b - a;
        });
        arrayNum.forEach(function (e) {
            oth.html(`<p>${e}</p>`)
        });


    Fiddle for clarity
    JavaScript Holden Alvarez, Jul 3, 2019

  • 1 Answers
  • 0
    const $elems = $('.numb');
    const values = $elems.get().map(n => n.dataset.price).sort((a, b) => b - a);
    $elems.each((i, n) => n.dataset.price = n.innerText = values[i]);


    or



    $('.numb').sort((a, b) => b.dataset.price - a.dataset.price).appendTo('body');
    Nora Conway

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!