toNatural: function()

in jspwiki-war/src/main/scripts/moo-extend/Array.NaturalSort.js [104:145]


    toNatural: function( column ){

        var len = this.length,
            num = [], dmy = [], kmgt = [], nat = [],
            val, i, isNode;

        for(i = 0; i < len; i++){

            //1. Retrieve the value to be sorted: native js value, or dom elements

            val = this[i];
            isNode = val && val.nodeType;

            //if "column" => retrieve the nth DOM-element or the nth Array-item
            if( !isNaN(column) ){ val = ( isNode ? val.getChildren() : val )[column]; }

            //retrieve the value and convert to string
            val = ("" + (isNode ? val.getAttribute("data-sortvalue") || val.textContent || val.get("title") : val)).trim();

            //2. Convert and store in type specific arrays (num, dmy, kmgt, nat)

            //CHECKME: some corner cases: numbers with leading zero's, confusing date string
            if( /(?:^0\d+)|(?:^[^\+\-\d]+\d+$)/.test(val) ){ num = dmy = 0; }

            //remove non numeric tail : replace( /[\w].*$/,'');
            if( num && isNaN( num[i] = +val ) ){ num = 0; }
            //if( num && isNaN( num[i] = + val.replace(/[\W].*$/,"") ) ){ num = 0; }

            if( nat && !( nat[i] = val.match(reNAT) ) ){ nat = 0; }

            //Only strings with non-numeric values
            if( dmy && ( num || isNaN( dmy[i] = Date.parse(val) ) ) ){ dmy = 0; }

            if( kmgt && isNaN( kmgt[i] = parseKMGT(val) ) ){ kmgt = 0; }

        }

        //console.log("[", kmgt ? "kmgt" : dmy ? "dmy" : num ? "num" : nat ? "nat" : "no conversion", "] ");

        return kmgt || dmy || num || nat || this.slice();

    },