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('');
};複製代碼