I am trying to sort some dynamically created Divs by date/time using JS/jQuery. There are 2 different types of divs (.note and .activity) which are using data-datetime="" or just datetime="" so I am attempting to sort them by the content in elements with the class name .timeago.
setTimeout(function() {
function sortDescending(a, b) {
var date1 = $(a).find(".timeago").text();
date1 = date1.split('/');
date1 = new Date(date1[2], date1[1] - 1, date1[0]);
var date2 = $(b).find(".timeago").text();
date2 = date2.split('/');
date2 = new Date(date2[2], date2[1] - 1, date2[0]);
return date1 < date2 ? 1 : -1;
};
$('.notes .note').sort(sortDescending).appendTo('.notes');
}, 3000);
.note { margin-bottom:30px;}
.note .timeago {color:red; font-weight:bold;}
.activity .timeago {color:green; font-weight:bold;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="notes panel-group" id="accordion">
<!------ Notes ------>
<div class="note tab-content panel panel-default" data-toggle="collapse" href="#collapse0">
<div class="row tab-content">
<div class="col-sm-3 metadata tab-content panel-heading">
<div class="postedon tab-content">
<abbr class="timeago" data-datetime="2022-10-30T18:54:39Z" title="10/30/2022 11:54 AM">10/30/2022 11:54 AM</abbr>
</div>
<div class="createdby text-muted">Should Be 1</div>
</div>
<div class="col-sm-9 content panel-collapse collapse" id="collapse0">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</div>
</div>
<div class="note tab-content panel panel-default" data-toggle="collapse" href="#collapse1">
<div class="row tab-content">
<div class="col-sm-3 metadata tab-content panel-heading">
<div class="postedon tab-content">
<abbr class="timeago" data-datetime="2022-08-11T15:39:41Z" title="8/11/2022 8:39 AM">8/11/2022 8:39 AM</abbr>
</div>
<div class="createdby text-muted">Should Be 5</div>
</div>
<div class="col-sm-9 content panel-collapse collapse" id="collapse1">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</div>
</div>
<div class="note tab-content panel panel-default" data-toggle="collapse" href="#collapse2">
<div class="row tab-content">
<div class="col-sm-3 metadata tab-content panel-heading">
<div class="postedon tab-content">
<abbr class="timeago" data-datetime="2022-10-05T14:53:01Z" title="10/5/2022 7:53 AM">10/5/2022 7:53 AM</abbr>
</div>
<div class="createdby text-muted">Should Be 2</div>
</div>
<div class="col-sm-9 content panel-collapse collapse" id="collapse2">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</div>
</div>
<!------ End Notes ------>
<!------ Activities ------>
<div class="activity note panel panel-default" data-toggle="collapse" href="#collapse3">
<div class="panel-heading tab-content">
<div class="createdby tab-content">Should Be 6</div>
<div class="postedon">
<time class="timeago" datetime="2022-04-03T15:41:09-07:00">4/3/2022 3:41 PM</time>
</div>
</div>
<div class="content panel-collapse collapse" id="collapse3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</div>
<div class="activity note panel panel-default" data-toggle="collapse" href="#collapse4">
<div class="panel-heading tab-content">
<div class="createdby tab-content">Should Be 3</div>
<div class="postedon">
<time class="timeago" datetime="2022-10-03T06:55:52-07:00">10/3/2022 6:55 AM</time>
</div>
</div>
<div class="content panel-collapse collapse" id="collapse4">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</div>
<div class="activity note panel panel-default" data-toggle="collapse" href="#collapse5">
<div class="panel-heading tab-content">
<div class="createdby tab-content">Should Be 4</div>
<div class="postedon">
<time class="timeago" datetime="2022-09-03T09:29:33-07:00">9/3/2022 9:29 AM</time>
</div>
</div>
<div class="content panel-collapse collapse" id="collapse5">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</div>
<!------ End Activities ------>
</div>
I have created a JS Fiddle for this – https://jsfiddle.net/bstime/oyec69g5/11/
3
Answers
Your date constructor params were incorrect so the date being parsed failed. I split the last date part (the year + time eg: 2022 9:29 AM) with space and get the first item to only get the year.
You can do that…
in case of invalid date values…
There is already a good plain Javascript solution, but since you are using jQuery, here is a jQuery option. It also seems clunky to me to have to use the text of your page for the sorting, when you do have an attribute which is presumably there explicitly for this kind of programmatic manipulation – eg maybe in future the text will change format and won’t be parseable in JS.