Created
April 24, 2015 07:27
-
-
Save liuyanghejerry/9fb42d47eb3274822b5c to your computer and use it in GitHub Desktop.
Locale aware sort, without serious test.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sort(a, b) { | |
for (var i = 0; i < a.length; i++) { | |
if (a[i] === b[i]) { | |
continue; | |
} | |
return isABeforeB(a[i], b[i]) ? 1 : -1; | |
} | |
} | |
function isAlpha(c) { | |
var charactor = c.charCodeAt(0); | |
return charactor < 256; | |
} | |
function localeCompare(a, b) { | |
return a.localeCompare(b); | |
} | |
function isABeforeB(a, b) { | |
if(isAlpha(a)) { | |
if (!b) { | |
return false; | |
} | |
if (isAlpha(b)) { | |
return a < b; | |
} | |
return true; | |
} else { | |
if (isAlpha(b)) { | |
return false; | |
} | |
return localeCompare(a, b); | |
} | |
} | |
console.log((['App无设备引导', 'App 闪屏页', '测试timestamp类型', '测试表单', '送货地址配置', '自动登录']).sort(sort).reverse()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment