Skip to content

Instantly share code, notes, and snippets.

@ishiduca
Created September 14, 2011 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishiduca/1215718 to your computer and use it in GitHub Desktop.
Save ishiduca/1215718 to your computer and use it in GitHub Desktop.
アルファベットの大文字小文字を区別しないソート
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