Last active
August 19, 2018 11:30
-
-
Save bang9/b12c2d6dfd613d1fb8f609e6fc97b6b5 to your computer and use it in GitHub Desktop.
sorting order by korean first
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
const obj = [ | |
{ | |
name:'48홍길동', | |
age:'23' | |
}, | |
{ | |
name:'Kim나나', | |
age:'18' | |
}, | |
{ | |
name:'15홍길동', | |
age:'23' | |
}, | |
{ | |
name:'김구라', | |
age:'39' | |
}, | |
{ | |
name:'Z하이', | |
age:'18' | |
}, | |
{ | |
name:'#ㅎㅎ', | |
age:'13' | |
} | |
] | |
function setPriority(obj){ | |
var char_ASCII = obj.name.charCodeAt(0); | |
//공백 | |
if (char_ASCII == 32) | |
return 0 | |
//특수기호 | |
else if ((char_ASCII>=33 && char_ASCII<=47) | |
|| (char_ASCII>=58 && char_ASCII<=64) | |
|| (char_ASCII>=91 && char_ASCII<=96) | |
|| (char_ASCII>=123 && char_ASCII<=126)) | |
return 1; | |
//숫자 | |
else if (char_ASCII >= 48 && char_ASCII <= 57 ) | |
return 2; | |
//영어(대문자) | |
else if (char_ASCII>=65 && char_ASCII<=90) | |
return 3; | |
//영어(소문자) | |
else if (char_ASCII>=97 && char_ASCII<=122) | |
return 4; | |
//한글 | |
else if ((char_ASCII >= 12592) || (char_ASCII <= 12687)) | |
return 5; | |
else | |
return -1; | |
} | |
obj.sort( (a,b)=>{ | |
if( this.setPriority(a) > this.setPriority(b) ) return -1 | |
if( this.setPriority(a) < this.setPriority(b) ) return 1 | |
return a.name < b.name ? -1 : a.name > b.name ? 1 : 0 | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment