Jquery sorting html table rows by date












0















I have been using a script to sort tables by a date string which I got from stackoverflow but the code has stopped working. May I kindly ask if anybody knows why?






$('tr.Entries').each(function() {
var $this = $(this),
t = this.cells[1].textContent.split('-');
$this.data('_ts', new Date(t[2], t[1] - 1, t[0]).getTime());
}).sort(function(a, b) {
return $(a).data('_ts') > $(b).data('_ts');
}).appendTo('tbody');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table-bordered" border="1">
<thead>
<tr class="Headers">
<th>Number</th>
<th>Date start
</th>
<th>Date end</th>
</tr>
</thead>
<tbody>
<tr class="Entries" data-id="13">
<td data-field-type="string">1234</td>
<td data-field-type="date">01-04-2015</td>
<td data-field-type="date">01-04-2015</td>
</tr>
<tr class="Entries" data-id="24">
<td data-field-type="string">1352</td>
<td data-field-type="date">04-10-2012</td>
<td data-field-type="date">23-10-2015</td>
</tr>
<tr class="Entries" data-id="8">
<td data-field-type="string">1124</td>
<td data-field-type="date">13-05-2014</td>
<td data-field-type="date">01-04-2015</td>
</tr>
<tr class="Entries" data-id="23">
<td data-field-type="string">1652</td>
<td data-field-type="date">07-11-2013</td>
<td data-field-type="date">22-10-2015</td>
</tr>
<tr class="Entries" data-id="23">
<td data-field-type="string">1652</td>
<td data-field-type="date">04-12-2013</td>
<td data-field-type="date">22-10-2015</td>
</tr>
</tbody>
</table>












share|improve this question

























  • Your code works perfectly, check your fiddle, and tell us what are you getting exactly? How it isn't working?

    – cнŝdk
    Nov 21 '18 at 7:33
















0















I have been using a script to sort tables by a date string which I got from stackoverflow but the code has stopped working. May I kindly ask if anybody knows why?






$('tr.Entries').each(function() {
var $this = $(this),
t = this.cells[1].textContent.split('-');
$this.data('_ts', new Date(t[2], t[1] - 1, t[0]).getTime());
}).sort(function(a, b) {
return $(a).data('_ts') > $(b).data('_ts');
}).appendTo('tbody');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table-bordered" border="1">
<thead>
<tr class="Headers">
<th>Number</th>
<th>Date start
</th>
<th>Date end</th>
</tr>
</thead>
<tbody>
<tr class="Entries" data-id="13">
<td data-field-type="string">1234</td>
<td data-field-type="date">01-04-2015</td>
<td data-field-type="date">01-04-2015</td>
</tr>
<tr class="Entries" data-id="24">
<td data-field-type="string">1352</td>
<td data-field-type="date">04-10-2012</td>
<td data-field-type="date">23-10-2015</td>
</tr>
<tr class="Entries" data-id="8">
<td data-field-type="string">1124</td>
<td data-field-type="date">13-05-2014</td>
<td data-field-type="date">01-04-2015</td>
</tr>
<tr class="Entries" data-id="23">
<td data-field-type="string">1652</td>
<td data-field-type="date">07-11-2013</td>
<td data-field-type="date">22-10-2015</td>
</tr>
<tr class="Entries" data-id="23">
<td data-field-type="string">1652</td>
<td data-field-type="date">04-12-2013</td>
<td data-field-type="date">22-10-2015</td>
</tr>
</tbody>
</table>












share|improve this question

























  • Your code works perfectly, check your fiddle, and tell us what are you getting exactly? How it isn't working?

    – cнŝdk
    Nov 21 '18 at 7:33














0












0








0








I have been using a script to sort tables by a date string which I got from stackoverflow but the code has stopped working. May I kindly ask if anybody knows why?






$('tr.Entries').each(function() {
var $this = $(this),
t = this.cells[1].textContent.split('-');
$this.data('_ts', new Date(t[2], t[1] - 1, t[0]).getTime());
}).sort(function(a, b) {
return $(a).data('_ts') > $(b).data('_ts');
}).appendTo('tbody');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table-bordered" border="1">
<thead>
<tr class="Headers">
<th>Number</th>
<th>Date start
</th>
<th>Date end</th>
</tr>
</thead>
<tbody>
<tr class="Entries" data-id="13">
<td data-field-type="string">1234</td>
<td data-field-type="date">01-04-2015</td>
<td data-field-type="date">01-04-2015</td>
</tr>
<tr class="Entries" data-id="24">
<td data-field-type="string">1352</td>
<td data-field-type="date">04-10-2012</td>
<td data-field-type="date">23-10-2015</td>
</tr>
<tr class="Entries" data-id="8">
<td data-field-type="string">1124</td>
<td data-field-type="date">13-05-2014</td>
<td data-field-type="date">01-04-2015</td>
</tr>
<tr class="Entries" data-id="23">
<td data-field-type="string">1652</td>
<td data-field-type="date">07-11-2013</td>
<td data-field-type="date">22-10-2015</td>
</tr>
<tr class="Entries" data-id="23">
<td data-field-type="string">1652</td>
<td data-field-type="date">04-12-2013</td>
<td data-field-type="date">22-10-2015</td>
</tr>
</tbody>
</table>












share|improve this question
















I have been using a script to sort tables by a date string which I got from stackoverflow but the code has stopped working. May I kindly ask if anybody knows why?






$('tr.Entries').each(function() {
var $this = $(this),
t = this.cells[1].textContent.split('-');
$this.data('_ts', new Date(t[2], t[1] - 1, t[0]).getTime());
}).sort(function(a, b) {
return $(a).data('_ts') > $(b).data('_ts');
}).appendTo('tbody');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table-bordered" border="1">
<thead>
<tr class="Headers">
<th>Number</th>
<th>Date start
</th>
<th>Date end</th>
</tr>
</thead>
<tbody>
<tr class="Entries" data-id="13">
<td data-field-type="string">1234</td>
<td data-field-type="date">01-04-2015</td>
<td data-field-type="date">01-04-2015</td>
</tr>
<tr class="Entries" data-id="24">
<td data-field-type="string">1352</td>
<td data-field-type="date">04-10-2012</td>
<td data-field-type="date">23-10-2015</td>
</tr>
<tr class="Entries" data-id="8">
<td data-field-type="string">1124</td>
<td data-field-type="date">13-05-2014</td>
<td data-field-type="date">01-04-2015</td>
</tr>
<tr class="Entries" data-id="23">
<td data-field-type="string">1652</td>
<td data-field-type="date">07-11-2013</td>
<td data-field-type="date">22-10-2015</td>
</tr>
<tr class="Entries" data-id="23">
<td data-field-type="string">1652</td>
<td data-field-type="date">04-12-2013</td>
<td data-field-type="date">22-10-2015</td>
</tr>
</tbody>
</table>








$('tr.Entries').each(function() {
var $this = $(this),
t = this.cells[1].textContent.split('-');
$this.data('_ts', new Date(t[2], t[1] - 1, t[0]).getTime());
}).sort(function(a, b) {
return $(a).data('_ts') > $(b).data('_ts');
}).appendTo('tbody');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table-bordered" border="1">
<thead>
<tr class="Headers">
<th>Number</th>
<th>Date start
</th>
<th>Date end</th>
</tr>
</thead>
<tbody>
<tr class="Entries" data-id="13">
<td data-field-type="string">1234</td>
<td data-field-type="date">01-04-2015</td>
<td data-field-type="date">01-04-2015</td>
</tr>
<tr class="Entries" data-id="24">
<td data-field-type="string">1352</td>
<td data-field-type="date">04-10-2012</td>
<td data-field-type="date">23-10-2015</td>
</tr>
<tr class="Entries" data-id="8">
<td data-field-type="string">1124</td>
<td data-field-type="date">13-05-2014</td>
<td data-field-type="date">01-04-2015</td>
</tr>
<tr class="Entries" data-id="23">
<td data-field-type="string">1652</td>
<td data-field-type="date">07-11-2013</td>
<td data-field-type="date">22-10-2015</td>
</tr>
<tr class="Entries" data-id="23">
<td data-field-type="string">1652</td>
<td data-field-type="date">04-12-2013</td>
<td data-field-type="date">22-10-2015</td>
</tr>
</tbody>
</table>





$('tr.Entries').each(function() {
var $this = $(this),
t = this.cells[1].textContent.split('-');
$this.data('_ts', new Date(t[2], t[1] - 1, t[0]).getTime());
}).sort(function(a, b) {
return $(a).data('_ts') > $(b).data('_ts');
}).appendTo('tbody');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table-bordered" border="1">
<thead>
<tr class="Headers">
<th>Number</th>
<th>Date start
</th>
<th>Date end</th>
</tr>
</thead>
<tbody>
<tr class="Entries" data-id="13">
<td data-field-type="string">1234</td>
<td data-field-type="date">01-04-2015</td>
<td data-field-type="date">01-04-2015</td>
</tr>
<tr class="Entries" data-id="24">
<td data-field-type="string">1352</td>
<td data-field-type="date">04-10-2012</td>
<td data-field-type="date">23-10-2015</td>
</tr>
<tr class="Entries" data-id="8">
<td data-field-type="string">1124</td>
<td data-field-type="date">13-05-2014</td>
<td data-field-type="date">01-04-2015</td>
</tr>
<tr class="Entries" data-id="23">
<td data-field-type="string">1652</td>
<td data-field-type="date">07-11-2013</td>
<td data-field-type="date">22-10-2015</td>
</tr>
<tr class="Entries" data-id="23">
<td data-field-type="string">1652</td>
<td data-field-type="date">04-12-2013</td>
<td data-field-type="date">22-10-2015</td>
</tr>
</tbody>
</table>






jquery html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 7:50









Mark Baijens

6,973103454




6,973103454










asked Nov 21 '18 at 6:55









James TanJames Tan

6




6













  • Your code works perfectly, check your fiddle, and tell us what are you getting exactly? How it isn't working?

    – cнŝdk
    Nov 21 '18 at 7:33



















  • Your code works perfectly, check your fiddle, and tell us what are you getting exactly? How it isn't working?

    – cнŝdk
    Nov 21 '18 at 7:33

















Your code works perfectly, check your fiddle, and tell us what are you getting exactly? How it isn't working?

– cнŝdk
Nov 21 '18 at 7:33





Your code works perfectly, check your fiddle, and tell us what are you getting exactly? How it isn't working?

– cнŝdk
Nov 21 '18 at 7:33












1 Answer
1






active

oldest

votes


















0














The compareFunction passed to sort needs to return an integer, if < is used, it's returning a boolean. The documentation mentions:





  • If compareFunction(a, b) is less than 0, sort a to an index lower than b, i.e. a comes first.

  • If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements.

  • If compareFunction(a, b) is greater than 0, sort b to an index lower than a, i.e. b comes first.




Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort



The change needed is replace $(a).data('_ts') < $(b).data('_ts') with $(a).data('_ts') - $(b).data('_ts') or $(b).data('_ts') - $(a).data('_ts') depending on the order you want.






share|improve this answer


























  • Why this got downvoted so quickly? :(

    – rvazquezglez
    Nov 21 '18 at 7:27











  • This is not the real problem, it's also working with > or <, check this updated fiddle.

    – cнŝdk
    Nov 21 '18 at 7:34













  • I see the same result on that fiddle. If < is changed to - the rows will be sorted by the value on first column .

    – rvazquezglez
    Nov 21 '18 at 7:43













  • What you stated about compareFunction callback is correct, but it's not the case here, It's working with < and > there's no need to use -, the code is already working, check this fiddle.

    – cнŝdk
    Nov 21 '18 at 7:47













  • It's using this.cells[1] the second column, that has dates, just by looking at the year they don't seem sorted (2015, 2012, 2014, 2013, 2013) the same order as the original html code.

    – rvazquezglez
    Nov 21 '18 at 7:54













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406721%2fjquery-sorting-html-table-rows-by-date%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The compareFunction passed to sort needs to return an integer, if < is used, it's returning a boolean. The documentation mentions:





  • If compareFunction(a, b) is less than 0, sort a to an index lower than b, i.e. a comes first.

  • If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements.

  • If compareFunction(a, b) is greater than 0, sort b to an index lower than a, i.e. b comes first.




Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort



The change needed is replace $(a).data('_ts') < $(b).data('_ts') with $(a).data('_ts') - $(b).data('_ts') or $(b).data('_ts') - $(a).data('_ts') depending on the order you want.






share|improve this answer


























  • Why this got downvoted so quickly? :(

    – rvazquezglez
    Nov 21 '18 at 7:27











  • This is not the real problem, it's also working with > or <, check this updated fiddle.

    – cнŝdk
    Nov 21 '18 at 7:34













  • I see the same result on that fiddle. If < is changed to - the rows will be sorted by the value on first column .

    – rvazquezglez
    Nov 21 '18 at 7:43













  • What you stated about compareFunction callback is correct, but it's not the case here, It's working with < and > there's no need to use -, the code is already working, check this fiddle.

    – cнŝdk
    Nov 21 '18 at 7:47













  • It's using this.cells[1] the second column, that has dates, just by looking at the year they don't seem sorted (2015, 2012, 2014, 2013, 2013) the same order as the original html code.

    – rvazquezglez
    Nov 21 '18 at 7:54


















0














The compareFunction passed to sort needs to return an integer, if < is used, it's returning a boolean. The documentation mentions:





  • If compareFunction(a, b) is less than 0, sort a to an index lower than b, i.e. a comes first.

  • If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements.

  • If compareFunction(a, b) is greater than 0, sort b to an index lower than a, i.e. b comes first.




Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort



The change needed is replace $(a).data('_ts') < $(b).data('_ts') with $(a).data('_ts') - $(b).data('_ts') or $(b).data('_ts') - $(a).data('_ts') depending on the order you want.






share|improve this answer


























  • Why this got downvoted so quickly? :(

    – rvazquezglez
    Nov 21 '18 at 7:27











  • This is not the real problem, it's also working with > or <, check this updated fiddle.

    – cнŝdk
    Nov 21 '18 at 7:34













  • I see the same result on that fiddle. If < is changed to - the rows will be sorted by the value on first column .

    – rvazquezglez
    Nov 21 '18 at 7:43













  • What you stated about compareFunction callback is correct, but it's not the case here, It's working with < and > there's no need to use -, the code is already working, check this fiddle.

    – cнŝdk
    Nov 21 '18 at 7:47













  • It's using this.cells[1] the second column, that has dates, just by looking at the year they don't seem sorted (2015, 2012, 2014, 2013, 2013) the same order as the original html code.

    – rvazquezglez
    Nov 21 '18 at 7:54
















0












0








0







The compareFunction passed to sort needs to return an integer, if < is used, it's returning a boolean. The documentation mentions:





  • If compareFunction(a, b) is less than 0, sort a to an index lower than b, i.e. a comes first.

  • If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements.

  • If compareFunction(a, b) is greater than 0, sort b to an index lower than a, i.e. b comes first.




Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort



The change needed is replace $(a).data('_ts') < $(b).data('_ts') with $(a).data('_ts') - $(b).data('_ts') or $(b).data('_ts') - $(a).data('_ts') depending on the order you want.






share|improve this answer















The compareFunction passed to sort needs to return an integer, if < is used, it's returning a boolean. The documentation mentions:





  • If compareFunction(a, b) is less than 0, sort a to an index lower than b, i.e. a comes first.

  • If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements.

  • If compareFunction(a, b) is greater than 0, sort b to an index lower than a, i.e. b comes first.




Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort



The change needed is replace $(a).data('_ts') < $(b).data('_ts') with $(a).data('_ts') - $(b).data('_ts') or $(b).data('_ts') - $(a).data('_ts') depending on the order you want.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 7:35

























answered Nov 21 '18 at 7:22









rvazquezglezrvazquezglez

1,0191323




1,0191323













  • Why this got downvoted so quickly? :(

    – rvazquezglez
    Nov 21 '18 at 7:27











  • This is not the real problem, it's also working with > or <, check this updated fiddle.

    – cнŝdk
    Nov 21 '18 at 7:34













  • I see the same result on that fiddle. If < is changed to - the rows will be sorted by the value on first column .

    – rvazquezglez
    Nov 21 '18 at 7:43













  • What you stated about compareFunction callback is correct, but it's not the case here, It's working with < and > there's no need to use -, the code is already working, check this fiddle.

    – cнŝdk
    Nov 21 '18 at 7:47













  • It's using this.cells[1] the second column, that has dates, just by looking at the year they don't seem sorted (2015, 2012, 2014, 2013, 2013) the same order as the original html code.

    – rvazquezglez
    Nov 21 '18 at 7:54





















  • Why this got downvoted so quickly? :(

    – rvazquezglez
    Nov 21 '18 at 7:27











  • This is not the real problem, it's also working with > or <, check this updated fiddle.

    – cнŝdk
    Nov 21 '18 at 7:34













  • I see the same result on that fiddle. If < is changed to - the rows will be sorted by the value on first column .

    – rvazquezglez
    Nov 21 '18 at 7:43













  • What you stated about compareFunction callback is correct, but it's not the case here, It's working with < and > there's no need to use -, the code is already working, check this fiddle.

    – cнŝdk
    Nov 21 '18 at 7:47













  • It's using this.cells[1] the second column, that has dates, just by looking at the year they don't seem sorted (2015, 2012, 2014, 2013, 2013) the same order as the original html code.

    – rvazquezglez
    Nov 21 '18 at 7:54



















Why this got downvoted so quickly? :(

– rvazquezglez
Nov 21 '18 at 7:27





Why this got downvoted so quickly? :(

– rvazquezglez
Nov 21 '18 at 7:27













This is not the real problem, it's also working with > or <, check this updated fiddle.

– cнŝdk
Nov 21 '18 at 7:34







This is not the real problem, it's also working with > or <, check this updated fiddle.

– cнŝdk
Nov 21 '18 at 7:34















I see the same result on that fiddle. If < is changed to - the rows will be sorted by the value on first column .

– rvazquezglez
Nov 21 '18 at 7:43







I see the same result on that fiddle. If < is changed to - the rows will be sorted by the value on first column .

– rvazquezglez
Nov 21 '18 at 7:43















What you stated about compareFunction callback is correct, but it's not the case here, It's working with < and > there's no need to use -, the code is already working, check this fiddle.

– cнŝdk
Nov 21 '18 at 7:47







What you stated about compareFunction callback is correct, but it's not the case here, It's working with < and > there's no need to use -, the code is already working, check this fiddle.

– cнŝdk
Nov 21 '18 at 7:47















It's using this.cells[1] the second column, that has dates, just by looking at the year they don't seem sorted (2015, 2012, 2014, 2013, 2013) the same order as the original html code.

– rvazquezglez
Nov 21 '18 at 7:54







It's using this.cells[1] the second column, that has dates, just by looking at the year they don't seem sorted (2015, 2012, 2014, 2013, 2013) the same order as the original html code.

– rvazquezglez
Nov 21 '18 at 7:54






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406721%2fjquery-sorting-html-table-rows-by-date%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?