Tiny Table Sorting (and common dates)

The current handling of the cp() function will cast any date beginning with a numeric char to a float. This means that sorting will not work for formats like "mm/dd/YYYY", "mm.dd.YYYY", etc. A quick fix would be to do a quick pattern match on the two variables to be sorted to check if they match that common format and avoid the parseFloat() handling altogether.


    function cp(f,c){

        var g,h; f=g=f.v.toLowerCase(), c=h=c.v.toLowerCase(), reg = /[0-9]{1,2}(\/|\.)[0-9]{1,2}(\/|\.)[0-9]{4}/i;

        if (!(f.match(reg) && c.match(reg))) {

            var i=parseFloat(f.replace(/(\$|\,)/g,'')), n=parseFloat(c.replace(/(\$|\,)/g,''));

            if(!isNaN(i)&&!isNaN(n)){g=i,h=n}

        }

        i=Date.parse(f); n=Date.parse(c);

        if(!isNaN(i)&&!isNaN(n)){g=i; h=n}

        return g>h?1:(g<h?-1:0)

    };

asked Apr 3, 2011 by anonymous

1 Answer

0 votes
sort day but not per month ;-(
answered Jul 12, 2012 by anonymous