Hi I cannot get sort function working on date. It changes order of the rows but it doesn't sort properly and dates are all over the place.
I would like upcoming events to future events.
See my code below.
$('tr.Entries').sort(function(a,b){
return new Date(jQuery(a).find('td.event-date > a').text()).getTime() <
new Date(jQuery(b).find('td.event-date > a').text()).getTime()
}).appendTo('tbody');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="event clearfix">
<thead>
<tr>
<td>Date</td>
<td>Event</td>
<td>Location</td>
<td>Ticket</td>
</tr>
<tr class="space"></tr>
</thead>
<tbody>
<tr class="Entries">
<td class="event-date">
<a href="#">25/08/2017</a>
</td>
<td class="event-title">
<a href="#">Live N Local Winter Music Festival</a>
</td>
<td class="event-location">
<a href="#">Pause Bar, Balaclava</a>
</td>
<td class="event-ticket-link">
<a href="#" class="button button-normal">BUY TICKET</a>
</td>
</tr>
<tr class="Entries">
<td class="event-date">
<a href="#">15/04/2017</a>
</td>
<td class="event-title">
<a href="#">Reggae, Seggae & Sega Night</a>
</td>
<td class="event-location">
<a href="#">Bar Open, Fitzroy Melbourne</a>
</td>
<td class="event-ticket-link">
<a href="#" class="button button-normal">BUY TICKET</a>
</td>
</tr>
<tr class="Entries">
<td class="event-date">
<a href="#">06/05/2016</a>
</td>
<td class="event-title">
<a href="#">The Sunraysia Multicultural Festival</a>
</td>
<td class="event-location">
<a href="#">Nowingi Place, Mildura Victoria</a>
</td>
<td class="event-ticket-link">
<a href="#" class="button button-normal">BUY TICKET</a>
</td>
</tr>
</tbody>
</table>
Thanks!
UPDATE
I have seemed to add dates into an array then sorted them - now I just too append them back to the body? Thanks
var dates = jQuery('tr.Entries').find('td.event-date > a').map(function() {
return jQuery(this).text();
}).get();
dates.sort(function(a,b){
var dateA = a;
dateA = dateA.replace(/(..)\/(..)\/(....)/, '$2-$1-$3');
var dateB = b;
dateB = dateB.replace(/(..)\/(..)\/(....)/, '$2-$1-$3');
return new Date(dateA).getTime() - new Date(dateB).getTime();
});
jQuery.each(dates, function (index, value) {
//how to append back to tbody?
console.log(value);
});
25/08/2017is not a valid date to be parsed withnew Date. MDN: Date, so yournew Datewill all be an invalid Date. Is your date always formatted that way, or does it depend on the region of the user?