数组排序方法

// 数组排序方法

function IsBigger(val1, val2)
{
    var bIsVal1Number = !isNaN(val1);
    var bIsVal2Number = !isNaN(val2);
    
    if(bIsVal1Number)
    {
        if(bIsVal2Number)
        {
            return (parseInt(val1) > parseInt(val2));
        }
        else
        {
            return false;
        }
    }
    else
    {
        if(bIsVal2Number)
        {
            return true;
        }
        else
        {
            return (val1 > val2);
        }
    }
}

Array.prototype.Sort = function()
{
    for(var i = 0; i < (this.length - 1); ++i)
    {
        var bSorted = false;
        for(var j = 0; j < (this.length - i - 1); ++j)
        {
            if(IsBigger(this[j], this[j + 1]))
            {
                tmp = this[j];
                this[j] = this[j+1];
                this[j+1] = tmp;
                bSorted = true;
            }
        }
        
        if(!bSorted)
        {
            break;
        }
    }
}

 

posted @ 2014-01-20 23:02  dzqabc  阅读(829)  评论(0编辑  收藏  举报