user1834809
user1834809

Reputation: 1311

jQuery sort elements using data id

I have an HTML structure as follows:

<div class="clist">
    <div data-sid=1></div>
    <div data-sid=2></div>
    <div data-sid=2></div>
    <div data-sid=1></div>
    <div data-sid=2></div>
    <div data-sid=2></div>
    <div data-sid=1></div>
</div>

I would like to sort them as:

<div class="clist">
    <div data-sid=1></div>
    <div data-sid=1></div>
    <div data-sid=1></div>
    <div data-sid=2></div>
    <div data-sid=2></div>
    <div data-sid=2></div>
    <div data-sid=2></div>
</div>

I am using the following function:

function sortContacts() {
    var contacts = $('div.clist'), cont = contacts.children('div');

    cont.detach().sort(function(a, b) {
        var astts = $(a).data('sid');
        var bstts = $(b).data('sid')
        return (astts > bstts) ? (astts > bstts) ? 1 : 0 : -1;
    });

    contacts.append(cont);
}

It is not working as expected.

It is working well for the first run but after adding new element or changing the data-sid attributes it no longer works.

Demo: http://jsfiddle.net/f5mC9/1/

Not working?

Upvotes: 47

Views: 75672

Answers (4)

Ram
Ram

Reputation: 144689

You can use dataset property which stores all of the custom data-* attributes of an element, it returns a string, in case that you want to convert the string to a number you can use parseInt or + operator.

$('.clist div').sort(function(a,b) {
     return a.dataset.sid > b.dataset.sid;
}).appendTo('.clist');

http://jsfiddle.net/CFYnE/

And your own code also work: http://jsfiddle.net/f5mC9/

Edit: Please note that IE10! and below do not support the .dataset property, if you want to support all browsers you can use jQuery's .data() method instead:

$('.clist div').sort(function(a,b) {
     return $(a).data('sid') > $(b).data('sid');
}).appendTo('.clist');

Upvotes: 81

LordAI
LordAI

Reputation: 1

function orderSteps() {
    var steps = $('div.steps'),
        cont = steps.children('div.step');
    cont.detach().sort(function (a, b) {
        var astts = a;
        var bstts = b;

        // stripping the id to get the position number
        var classArrA = $(astts).attr("id").split("-")[1];
        var classArrB = $(bstts).attr("id").split("-")[1];

        // checking for the greater position and order accordingly
        if (parseInt(classArrA) > parseInt(classArrB)) {
            return (0)
        }
        else {
            return (-1)
        }
    })
    steps.append(cont);
};

orderSteps()
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container-fluid steps">
            <div class="row text-center border-bottom step" id="step-1">
                <span class="col-3">Item 1</span>
            </div>
            <div class="row text-center border-bottom step" id="step-2">
                <span class="col-3">Item 2</span>
            </div>
            <div class="row text-center border-bottom step" id="step-30">
                <span class="col-3">Item 3</span>
            </div>
            <div class="row text-center border-bottom step" id="step-4">
                <span class="col-3">Item 4</span>
            </div>
        </div>

Upvotes: 0

devside
devside

Reputation: 2217

$('.clist div').sort(function(a,b) {
     return parseInt(a.dataset.sid) - parseInt(b.dataset.sid);
}).appendTo('.clist');

Upvotes: 15

Franc
Franc

Reputation: 5496

A more generic function to sort elements using jQuery:

$.fn.sortChildren = function (sortingFunction: any) {

    return this.each(function () {
        const children = $(this).children().get();
        children.sort(sortingFunction);
        $(this).append(children);
    });

};

Usage:

$(".clist").sortChildren((a, b) => a.dataset.sid > b.dataset.sid ? 1 : -1);

Upvotes: 6

Related Questions