Skip to content

Instantly share code, notes, and snippets.

@flyskyne
Created January 5, 2017 06:48
Show Gist options
  • Select an option

  • Save flyskyne/b6b310187ec26581f7ee548656473e72 to your computer and use it in GitHub Desktop.

Select an option

Save flyskyne/b6b310187ec26581f7ee548656473e72 to your computer and use it in GitHub Desktop.
한글부터 정렬
function hangulFirstCompare(a, b) {
function addOrderPrefix(s) {
var code = s.toLowerCase().charCodeAt(0);
var prefix;
// 한글 AC00—D7AF
if (0xac00 <= code && code <= 0xd7af) prefix = '1';
// 한글 자모 3130—318F
else if (0x3130 <= code && code <= 0x318f) prefix = '2';
// 영어 소문자 0061-007A
else if (0x61 <= code && code <= 0x7a) prefix = '3';
// 그외
else prefix = '9';
return prefix + s;
}
a = addOrderPrefix(a);
b = addOrderPrefix(b);
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
}
var array = ["123", "asbe", "가다다", "가나다", "신승엽", "ㅋㅋㅋ", "나라", "나가라"];
array.sort(hangulFirstCompare);
console.log(array);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment