absolute.js and date sorting
absolute.js and date sorting

I am using moment.js and datetime-moment.js with $.fn.dataTable.moment("DD/MM/YYYY"); declared in my js. All works fine and the dates sort correctly.
Then I installed absolute.js and I’ve tried both namesType and numbersType to sort the date. The blank spaces are moved to the bottom as expected, but the dates aren’t sorting in order. They are now sorting by the day only.
E.g.
01/10/2020
10/07/2020
16/08/2020
24/09/2020
So my question is, before creating a test, do these plugins work together and if they do is there something I can do to fix the sorting? If there is already a demo of the 2 scripts successfully working together then that would be great.
This question has accepted answers - jump to:
Answers
The Datatablees type detection won't allow for multiple types to be assigned to a column. With the datetime-moment.js plugin the type detection will validate that all cells in the column are the same. Meaning it will make sure all the cells in the column match the format
DD/MM/YYYY
for your case. If not it will set the column type to string.Basically the two plugins will need to be combined into one to allow for the sorting the way you want.
I don't know of any examples of the sorting the way you want.
Kevin
Thank you @kthorngren. I sort of guessed I might have to choose one plugin or the other after testing and not being able to find any examples myself. I appreciate your reply clarifying this. I'll just stick with datetime-moment.js for the ability to sort dates correctly which is more important to me.
I guess on thing you could try is to use orthogonal data and set a date for each of the non-date values, like
01/01/0001
for example.I haven't tried this so it may or may not work and may need changes to work but something like this:
The idea is to modify the string to a date value that will sort the way you want. Also I expect it will affect the type detection to match your moment format.
Kevin
I tried to experiment with that but couldn't get it to work. I can see how it should work in this situation though because I've used orthogonal data before. I replaced
if (data === 'my none date value')
and tried bothif (data === '')
andif (data === null)
. Should there just be onereturn data;
? I also got the error for the column "Requested unknown parameter".I thought my code snippet looked funny. Its missing a
}
between lines 6 and 7, like this:Here is a running example:
http://live.datatables.net/tusaxitu/1/edit
The function always needs to return something.
Kevin
I saw that and added it, but then I removed the 2nd
return data;
Unfortunately this isn't going to work in my implementation because columns will be a mixture of dates and null values. I could add a space but it would probably be best to leave the sorting as it is.
Thanks @kthorngren for helping. I've now learnt more about orthogonal data so it's still been beneficial.
You can check for whatever data or type you want. I simply used a sting for the example.
Kevin
But when I tried both
if (data === '')
andif (data === null)
that didn't work. Is this incorrect?They both work here:
http://live.datatables.net/tusaxitu/2/edit
I guess it depends on what your data really is. Maybe its not a blank string (
""
) or anull
but something else. PLease provide a link to your page or update the test case or create your own showing the data that you have so we can help.Kevin
Hi Kevin. I think we are at cross purposes and moved away from the original problem. Your code does work fine for the sorting, but it was always keeping the spaces at the bottom which I was trying to achieve which has caused the confusion. I'll stick with the normal behaviour for datetime-moment.js and just use absolute.js on non-date format columns.