Created
September 14, 2011 02:22
-
-
Save ishiduca/1215718 to your computer and use it in GitHub Desktop.
アルファベットの大文字小文字を区別しないソート
This file contains hidden or 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 qw (str, cut) { | |
if (typeof str !== 'string') return null; | |
cut = cut || /\s+/; | |
cut = (typeof cut === 'string') ? new RegExp(cut) : cut; | |
return str.replace(/^\s+/,'').replace(/\s+$/,'').split(cut); | |
} | |
var ARRY = qw( " G E f b a D c " ); | |
console.log("元の並び"); | |
console.log(JSON.stringify(ARRY)); | |
console.log("デフォルトソートしたあとの並び"); | |
ARRY.sort(); | |
console.log(JSON.stringify(ARRY)); | |
console.log("大文字小文字を区別しないソートしたあとの並び"); | |
ARRY.sort(function (a, b) { | |
a = a.toString().toLowerCase(); | |
b = b.toString().toLowerCase(); | |
return (a > b) ? 1 : | |
(b > a) ? -1 : 0; | |
}); | |
console.log(JSON.stringify(ARRY)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment