把字符按ascii码排序

来源:互联网 发布:多伦多大学gpa算法 编辑:程序博客网 时间:2024/05/21 19:45
function asciSort(targ) {    //字符串有方法charCodeAt,把字符转为ascii码    var str = '',        toAscFn = str.charCodeAt,        _tempArr = targ ? targ.split('') : [],        i = 0,        j,        _temp;    if (_tempArr <= 1)        return targ;    for (; i < _tempArr.length; i++) { //冒泡算法        for (j=0; j < _tempArr.length- i-1; j++) {            //pre = _tempArr[j];            //current = _tempArr[j + 1]            if (toAscFn.apply(_tempArr[j]) < toAscFn.apply(_tempArr[j + 1])) {                _temp = _tempArr[j];                _tempArr[j] = _tempArr[j + 1];                _tempArr[j + 1] = _temp;            }            //console.log(_tempArr);        }    }    return _tempArr.join('');};

原创粉丝点击