I’ve been trying to sort my dates by ascending/descending order in jQuery but with no luck. Any idea to what is going wrong? Here is my code: http://jsfiddle.net/UdwNh/
var itemsArray = $('div#activity_box');
itemsArray.sort(function(a,b){
var aTime = new Date($(a).find('#activity_date').text()).getTime();
var bTime = new Date($(b).find('#activity_date').text()).getTime();
return aTime - bTime;
});
$('#sortAsc').click(function(){
$("#wrapper").empty();
$(itemsArray).each(function(){
$("#wrapper").prepend($(this));
});
});
$('#sortDesc').click(function(){
$("#wrapper").empty();
$(itemsArray).each(function(){
$("#wrapper").append($(this));
});
});
Relevant HTML:
<input class="btn" type="button" value="Sort Ascending" id="sortAsc"/>
<input class="btn" type="button" value="Sort Descending" id="sortDesc"/>
<div id="wrapper">
<ul>
<li class='item'><div id='activity_date'>01/10/2012</div>
<div id='activity_box'>
<div id='activity_text' class='windowClass2'>...text here...</div>
</div></li><li class='item'><div id='activity_date'>04/10/2012</div>
<div id='activity_box'>
<div id='activity_text' class='windowClass2'>...text here...</div>
</div></li><li class='item'><div id='activity_date'>10/10/2012</div>
<div id='activity_box'>
<div id='activity_text' class='windowClass2'>...text here...</div>
</div></li><li class='item'><div id='activity_date'>16/10/2012</div>
<div id='activity_box'>
<div id='activity_text' class='windowClass2'>...text here...</div>
</div></li>
</ul>
</div>
There are several issues with that code.
You’re using the same
idvalue on more than one element.idvalues must be unique on the page.To fix it, use a class value instead, or a
data-*attribute, or anything else that’s allowed to occur more than once on a page.You’re calling
sorton a jQuery instance. jQuery has no documentedsortfunction. It may have an undocumented one, but if so, you have no idea what it does. It’s certainly doesn’t make sense to assume it will rearrange DOM elements.To fix it, you’ll have to write your own function for that (it’s not complicated), or find a plug-in that does it for you.
Even if
sortdid work, the date strings you’re passing intonew Date(string)are in the form20/10/2012,17/10/2012, and such. That isn’t a format thatnew Date(string)is documented to understand. The only string format thatnew Date(string)is documented to understand is a simplified form of ISO-8601, which doesn’t look like that. And in fact,new Date("20/10/2012")fails on Chrome and probably other browsers.To fix it, you’ll need to get the dates another way. You could store the milliseconds-since-the-Epoch value on the elements as a
data-*attribute and then use it directly. Or you could parse the date strings you have yourself. If they’re always in thatdd/mm/yyyyformat, parsing the individual parts into numbers is trivial, and then you can pass the resulting numbers for the year, month, and day intonew Date(year, month, date). (Remember that months start with0= January.)You’re putting in a closing
</li>in at the outset, where there’s no opening<li>.To fix it, don’t do that. 🙂