Skip to content

Instantly share code, notes, and snippets.

@jikeytang
Last active August 29, 2015 14:04
Show Gist options
  • Save jikeytang/454cbb566e69e3b9c061 to your computer and use it in GitHub Desktop.
Save jikeytang/454cbb566e69e3b9c061 to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140728-题目1
琴琴是一个具备高情商与高智商"双重杀手",她想根据注册时间较早车牌的的车,
来陪她一块去海边看日出。经过研究发现,车牌也是按序列发放,
由5个字母或数字组成,每个位排序是按字母数字的ASCII的先后位置,
比如:0o4r4 > ye6er ,0的ASCII是48,y的ASCII是121。所以0o4r4是较早的,
如果第一位相同,则判断第2位。实现这个程序。
PS:
1. 回复时注意加上下面这句话,才会有语法高亮或格式缩进。
```javascript
// you code
```
2. 粘贴代码时请使用shift+tab,缩进前面的空白。
@replace5
Copy link

function sortByAscii() {
    var args = [].slice.call(arguments, 0);
    return args.sort(function(n1, n2, i) {
        n1 = String(n1);
        n2 = String(n2);
        i = i || 0;
        len1 = n1.length;
        len2 = n2.length;
        return Math.min(len1, len2) == i ? len2 - len1 : n1.charCodeAt(i) - n2.charCodeAt(i) || arguments.callee(n1, n2 , ++i);
    });
}

@webjyh
Copy link

webjyh commented Jul 28, 2014

var fun = function( index, val1, val2 ){
    var len = val1.length - 1,
        item1 = val1.charCodeAt( index ),
        item2 = val2.charCodeAt( index );
    if ( item1 == item2 ){
        if ( index >= len ){
            return;
        } else {
            arguments.callee( index+1, val1, val2 );
        }
    } else {
        ( item1 > item2 ) ? console.log( val2 + ' > ' + val1 ) : console.log( val1 + ' > ' + val2 );
    }
};
fun( 0, '0o4r4', 'ye6er' );

@replace5
Copy link

@webjyh sort的算法好像有点不太一样的

@webjyh
Copy link

webjyh commented Jul 28, 2014

@feng524822 好吧。。。

@karrynew
Copy link

var str1="0o4r4";
var str2="ye6er";
function compareMin(str1,str2){
    var flag=true;
    for(var i=0,ii=str1.length;i<ii;i++){
        if(str1.charCodeAt(i)>str2.charCodeAt(i)){
            flag=false;
            return (str1+"比"+str2+"要晚");
        }else if(str1.charCodeAt(i)<str2.charCodeAt(i)){
            flag=false;
            return (str1+"比"+str2+"要早");
        }
    };
    if(flag){
        return "一样的";
    }
};
compareMin(str1,str2);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment