2

I have a bunch of span4 class elements in my html. they look something like this:

    <div class="span4">
        <div class="widget">
            <div class="header">blablabla</div>
        </div>
    </div>

I want to sort the span4 by that text iside header class.

I do this to sort them

$(".span4").sort(sortAlpha)

but how do I select the text inside the header class?

I'm doing this but I guess there is a better way

    function sortAlphaAsc(a,b){  
        var nomeA = $(a.childNodes[1].childNodes[1]).text();
        var nomeB = $(b.childNodes[1].childNodes[1]).text();
        return a.innerHTML.toLowerCase() > b.innerHTML.toLowerCase() ? 1 : -1;  
    };

there must be a better way than

$(a.childNodes[1].childNodes[1]).text()
3
  • 1
    $('.span4').find('.header').text()? Commented Dec 14, 2012 at 16:26
  • 2
    How about a.find('.header').text()? Commented Dec 14, 2012 at 16:26
  • You don't use that variables in your return statement, simply remove them :). Commented Dec 14, 2012 at 16:26

4 Answers 4

3
var elems = $(".span4");

elems.sort(function(a, b) {
    return $(a).find('.header').text().toUpperCase().localeCompare(
         $(b).find('.header').text().toUpperCase()
    );
});

$(".span4").parent().html(elems);​

FIDDLE

Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

function sortAlphaAsc(a,b){  
    var nomeA = $(a).find('div.header').text();
    var nomeB = $(b).find('div.header').text();
    return nomeA.toLowerCase() > nomeB.toLowerCase();  
};

2 Comments

Also, I think you can get away with losing the ternary: return a.innerHTML.toLowerCase() > b.innerHTML.toLowerCase();
@undefined, I just copied the OP's code and fixed the part they were asking about. I corrected it.
2

You could detach the spans, sort and append them. That will be very fast too as changing elements in memory and only updating the DOM once in the end is very efficient.

var $spans = $(".span4").detach();

var sortedSpans = $spans.sort(function(spanA, spanB) {
    var spanTextA = $("div.header", spanA).text();
    var spanTextB = $("div.header", spanB).text();
    
    return spanTextA > spanTextB;
});

$("body").append(sortedSpans);

Obviously instead of body you append it back to it's actual container element.

Or if the spans are in a common container store the parent in cache var $parent = $spans.parent() and in the end simply do $parent.html(sortedSpans).

I don't know your whole mark-up but that should get you started.

DEMO - Detach spans, sort them and append again

1 Comment

like the other solutions, you got what i wanted ;) thanks for the help!
1

Do you mean something like this:

$('.span4').find('.header').text();

This will return the text inside the header div.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.