Another couple of sorting questions:
Another couple of sorting questions:
Belisarius
Posts: 26Questions: 0Answers: 0
I have a column of dates but it doesn't seem to sort by date (dd/mm/yyy). Is there something I need to set for that column to enable this?
I also have a column with format: d/xxx/ddd where d=1-0 and x=A-Z. I need to be able to sort on this columns basically on each part in turn with the d and ddd being integers and the xxx text. Suppose this would be similar to a date dd/mmm/yyyy sort?
I've looked at some of the alternate sorting solutions but I think I'm missing the basics of where to start with this one.
Any help much appreciated.
Cheers
I also have a column with format: d/xxx/ddd where d=1-0 and x=A-Z. I need to be able to sort on this columns basically on each part in turn with the d and ddd being integers and the xxx text. Suppose this would be similar to a date dd/mmm/yyyy sort?
I've looked at some of the alternate sorting solutions but I think I'm missing the basics of where to start with this one.
Any help much appreciated.
Cheers
This discussion has been closed.
Replies
Easiest way to use hidden column and sort on that?
Similar for other column?
The key with using a sort function to sort dates which can't be parsed by Date().parse() is to transform the date into a number which can be sorted on (for example Unix time). Have a look at the thread here which discusses sorting dates: http://datatables.net/forums/comments.php?DiscussionID=81&page=1
Allan
I'm now trying to get the dd/mm/yyyy date to work. I've written an sType function similar to above but it seems to be setting the default sType to 'date' as it recognises e.g. 01/04/2009 as a valid javascript date (4th Jan 2009) and so never calls my functions. Is there anyway I can get it to call my sType function before the defaults?
Assuming that is what is happening....
Also worth noting that "01/04/2009" will only be captured by Javascript Date().parse() under certain circumstances (i believe it is non-standard) - internalisation can cause problems here - for example I read it as 1st April 2009 ;-)
Allan
I've since switched the date to be dd-mmm-yyyy again that seems to work without the sType value as well?
Cheers
The Date().parse() built into most browsers can be quite fiddly because some browser makers extend what it can parse beyond what the specs say - so it can be quite hard to pin this kind of thing down. So yes, it is indeed quite possible that these formats will parse (I would expect dd-mmm-yyyy to certainly). It might be worth having a look at:
MDC: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date/parse
ECMA Spec: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf - section 15.9.4.2
Allan
You need an entry in your datatable init code like:
[code]
"aoColumns": [
{ "bSortable": true, "sType": "box_ref"},
{ "bSortable": false , "bSearchable": false },
{ "bSortable": true, "sType": "uk_date2" },
{ "bSortable": false , "bSearchable": false },
{ "bSortable": false , "bSearchable": false },
{ "bSortable": false , "bSearchable": false },
{ "bSortable": false , "bSearchable": false }
]
[/code]
and then the following functions:
[code]
jQuery.fn.dataTableExt.aTypes.push(
function ( sData )
{
if (sData.match(/^(0[1-9]|[12][0-9]|3[01])\-(0[1-9]|1[012])\-(19|20|21)\d\d$/))
{
return 'uk_date2';
}
else
{
return null;
}
}
);
jQuery.fn.dataTableExt.oSort['uk_date2-asc'] = function(a,b) {
var ukDatea = a.split('/');
var ukDateb = b.split('/');
var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['uk_date2-desc'] = function(a,b) {
var ukDatea = a.split('/');
var ukDateb = b.split('/');
var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
[/code]
I'd appreciate any comments if it's not right
Cheers
NB Just seen some more info now in the Developer Section for this so hopefully it's right!
Looks great to me! Nice one :-) Do you mind if I include this on the plug-ins page? If not, do you have a web-site you want me to point a link at (and name?).
One thing worth noting is that if the type detection is working, then there should be no needed to explicitly defined 'sType' for the matching column. The idea being that it's not always ideal to define aoColumns (although in your case it looks like it required due to the 'box_ref' type).
Allan
re the type detection. I think that was my point from above. What sequence does it check them? If it hits the javascript default date first then it will id that and use that (although only in cases where the dd/mm and mm/dd are both valid) else for something like 31/12 it'll skip to my type and use that correctly - unless you specify the sType for the column?
Cheers
Andy
Allan
Have a look at the plug-ins example: http://datatables.net/examples/example_sorting_plugin.html . This should show you how you can add plug-ins.
The other examples (which are all linked from http://datatables.net/usage ) can be quite useful as well.
Allan
http://www.datatables.net/examples/api/dom_sort.html
and
http://datatables.net/examples/example_sorting_plugin.html
have they been moved?
http://datatables.net/examples/plug-ins/dom_sort.html
http://datatables.net/examples/plug-ins/sorting_plugin.html
No example has been removed yet, so they are all available from http://datatables.net/examples/ .
Allan