0

I'm using JQuery datatables plugin and works really well. Problem I have is sorting a date field. The date field is the first field in the table.

For a better UX (because day of the week is an important factor) I want to show dates in ascending order like this:

Fri 4th July 2014
Thu 10th July 2014
Fri 18th July 2014

When I load the table into datatables it sort the field thus:

Fri 18th July 2014
Fri 4th July 2014
Thu 10th July 2014

Which is alpha numeric. Not good for dates.

Is there any way I can add a field to the <td> e.g. <td data-sort="2014-05-06"> and have it sort according to that variable instead of just the cell contents for that particular column?

I have searched their docs and API but can't see an immediate solution.

2
  • see datatables.net/examples/advanced_init/html5-data-attributes.html use data-order=(date in timestamp format) Commented Jul 21, 2014 at 11:46
  • That's exactly it! Many thanks - if you want to add it as answer I'll accept Commented Jul 21, 2014 at 11:51

2 Answers 2

1

Use data-order=(date in timestamp format)

<td data-order="1332975600">Thu 29th Mar 12</td>

Refer: http://datatables.net/examples/advanced_init/html5-data-attributes.html

Sign up to request clarification or add additional context in comments.

Comments

1

The answer is actually in my question... I guessed what the solution should be without trying it as I couldn't find any reference to it.

tl;dr

<td data-sort="2014-05-06">

Many thanks @TamilSelvan

You can also use timestamp as suggested below

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.