views:

111

answers:

4

Hi, how could I arrange the following div's by the value in the attribute amount with jquery? Thanks.

<a href="#" id="id1" class="lTest" amount="12">
    <div>abcd1</div>
<a/>

<a href="#" id="id2" class="lTest" amount="64">
    <div>abcd2</div>
<a/>

<a href="#" id="id3" class="lTest" amount="32">
    <div>abcd3</div>
<a/>

<a href="#" id="id4" class="lTest" amount="8">
    <div>abcd4</div>
<a/>
A: 

Try this:

var links = $('a.lTest').sort( function(a,b) { return $(a).attr('amount') - $(b).attr('amount'); } );

for ( var i = 1; i < links.length; i++ ) {
    links.eq(i-1).insertBefore( links.eq(i) );
}
Tinister
I ran the code and firebug tells me $("a.lTest").sort is not a function
usertest
I've been able to run sort on jQuery objects fine. What about if you try `jQuery("a.lTest").sort(...)`?
Tinister
Still the same error, thanks the help but it doesn't seem to work.
usertest
I think this is realy a stupid question. take a answer and stick with it. Thast my opinion. Every answer here would work, but you are not able to test it, nor give more information what you realy need. And doing some jquery function calls without giving an id as paramater will never work. On your code i saw no where an id, jusa$t a class lTest.
streetparade
I've tested everyone of the suggestions they just don't seem to work. But i've taken your suggestion and added id's if that'll help.
usertest
A: 

first, assume the tags are surrounded by a div (id ="testDiv")

Sorry, my initial way of doing this is wrong...

DO NOT DO THIS: BROKEN!!! (See edit below)

var testDiv = $('#testDiv'); var children = testDiv.children('.lTest'); children.each(function() {$(this).remove();}); var testArray = $.makeArray(children); testArray.sort(function(a,b){ return parseInt($(a).attr('amount')) - parseInt($(b).attr('amount'); });

children.each(function() {testDiv.append(this);});

EDIT: THIS IS WORKING VERSION:

var testDiv = $('#testDiv');
var children = testDiv.children('.lTest').remove();

children = children.sort(function(a,b){
  return parseInt($(a).attr('amount')) - parseInt($(b).attr('amount'));
});

testDiv.append(children);
JBristow
Thanks, but I ran the code with the elements inside the #testDiv you suggested but it still doesn't work. I cannot figure out what i'm missing. Did you test the code on your system?
usertest
whoops, my bad, I was running off of what I remembered writing before... I will edit with working example.
JBristow
+1  A: 

This one should work.

<!doctype html>
<html>
    <head>
        <script src='jquery.js'></script>
        <script>
            $(document).ready(init);

            function init() {
                var parent = $('#someid');
                var children = $('a', parent);
                children.sort(function(a, b) {
                   return parseInt($(a).attr('amount')) - parseInt($(b).attr('amount'));
                })
                $.each(children, function(i, child) {
                    parent.append(child);
                });
            }
        </script>
    </head>
    <body>
        <div id="someid">
            <a href="#" class="lTest" amount="12"><div>abcd2</div></a>
            <a href="#" class="lTest" amount="64"><div>abcd4</div></a>
            <a href="#" class="lTest" amount="32"><div>abcd3</div></a>
            <a href="#" class="lTest" amount="8"><div>abcd1</div></a>
        </div>
    </body>
</html>

[Edit] Replaced by a SSCCE to prove it's working. I've changed x in abcdx to the expected ordering. You should end up with abcd1, abcd2, abcd3 and abcd4 in this order.

BalusC
This doesn't seem to work. Considering several helpful people commented and I haven't been able to get any of it to work, I'm assuming the problems on my end. @BalusC, have you tested the above code with with the markup I posted. Did it work? Thanks.
usertest
You used <a/> to close <a> tags. This is wrong. Fix it first.
BalusC
I edited the example to the SSCCE flavor.
BalusC
Thanks, I'm sure the other examples would work too were it not for some strange problem with my pc. Thanks everyone else too.
usertest
A: 

Your first comment on Tinister's answer indicates that your test page doesn't know anything about JQuery. Make sure you are properly including JQuery in your tests and that the HTML is correctly constructed. Then you will probably find that one of the other answers works.

John

John Fisher