1

Surely the problem is not in function, but as the following code shows, it is necessary that you click two times to sort it in descending ~ growing. How can I fix this?

Here is a jsFiddle example.

I believe that the error in this :

function lxp(a, b){
    var adate = new Date($(a).attr("data-date"));
    var bdate = new Date($(b).attr("data-date"));
    if(tipo == 'acrescente'){
        return adate > bdate ? -1 : 1;
    }else if(tipo == 'decrescente'){
        return adate > bdate ? 1 : -1;
    }
}
George Cummins
  • 28,485
  • 8
  • 71
  • 90
  • Thanks for adding your code. You really should take the time to proofread your question before you post it. – Asad Saeeduddin May 31 '13 at 18:52
  • Yes, I'm used to traditional forums and ended up doing this, I'm sorry. – Douglas Silva May 31 '13 at 18:58
  • jQuery doesn't have a `sort()` method. [This question](http://stackoverflow.com/questions/1134976/how-may-i-sort-a-list-alphabetically-using-jquery) is similar to yours, however. – Blazemonger May 31 '13 at 19:06
  • No. I'm doing it with date and not words, and the problem is specific to what I'm using, not generalized "how-to" ... – Douglas Silva May 31 '13 at 19:12
  • 1
    @Blazemonger, jQuery *does* have a `sort` method, but it's undocumented and equivalent to `Array.prototype.sort`. – zzzzBov May 31 '13 at 19:16

4 Answers4

1

This seems like the most straight forward approach to me:

http://jsfiddle.net/3T5kN/11/

$(function(){
    var tllp = 15;
    $('#blocoSite li').each(function(i, lep){
        $(lep).css({ top : tllp });
        tllp += 15;
    });
});

$(document).ready(function() {

    var tipo = "decrescente";
    $('#ordeData').click(function() { 
        tipo = tipo == "acrescente" ? "decrescente" : "acrescente"
        var nposY = 0;

        function lxp(a, b){
            var adate = new Date($(a).attr("data-date"));
            var bdate = new Date($(b).attr("data-date"));

            if(tipo == 'acrescente'){
                return adate > bdate ? -1 : 1;
            }else if(tipo == 'decrescente'){
                return adate < bdate ? -1 : 1;
            }
        }

        $("#blocoSite li").sort(lxp).each(function(i, el){
            nposY = i * 15;

            $(this).animate({
                left: 200,
                top :  nposY
            }, 800);
        });

    })
})

With accompanying HTML

<ul id="blocoSite">
    <li data-date="2010-05-12">2010</li>
    <li data-date="2012-05-12">2012</li>
    <li data-date="2015-05-12">2015</li>
    <li data-date="2008-05-12">2008</li>
    <li data-date="2009-05-12">2009</li>
    <li data-date="2010-05-12">2010</li>
</ul>
<button id="ordeData">CLICK</button>
sberry
  • 128,281
  • 18
  • 138
  • 165
0

Try adding a 0 to the months smaller than 10 like <li data-date="2010-05-12">instead of <li data-date="2010-5-12">

I did that in your fiddle a the list get sorted at the first click

EDIT: updated fiddle (http://jsfiddle.net/3T5kN/8/)

I get this. It's no ok?

  • 2015
  • 2012
  • 2010
  • 2010
  • 2009
  • 2008
Matías Cánepa
  • 5,770
  • 4
  • 57
  • 97
0

I was getting "Invalid date" errors from adate and bdate, so I turned your date-strings into numbers (using parseInt) and it works.

http://jsfiddle.net/3T5kN/7/

thv20
  • 986
  • 4
  • 13
  • 26
0

Replace the javascriptcode with:

$(function(){
    var tllp = 15;
    $('#blocoSite li').each(function(i, lep){
        $(lep).css({ top : tllp });
        tllp += 15;
    });
});

function orderDate(tipo){
    $('#ordered').remove();
    var nposX = 0;
    var nposY = 0;

    if(tipo == 'acrescente'){
        $("#ordeData").attr({'onclick' : 'orderDate("decrescente");'});
    }else if(tipo == 'decrescente'){
        $("#ordeData").attr({'onclick' : 'orderDate("acrescente");'});
    }


    function lxp(a, b){
        var adate = new Date($(a).attr("data-date"));
        var bdate = new Date($(b).attr("data-date"));
        if(tipo == 'acrescente'){
            return adate > bdate ? -1 : 1;
        }else if(tipo == 'decrescente'){
            return adate < bdate ? -1 : 1;
        }
    }
    var order="";
    nposY = 0;
    $("#blocoSite li").sort(lxp).each(function(i, el){
        order += el.outerHTML+"\n";
        nposY += 15;

        $(this).animate({
            left: 200,
            top :  nposY
        }, 800);
    });
    $('#ordered').html(order);
}

This way, you will put the list-items in the sorted order, rather than one based on the old position (in addition to the sort) of the item.

ljgw
  • 2,751
  • 1
  • 20
  • 39