In JavaScript there is a sort function for arrays. Though, the result will take into account the case of each character. Therefore you can have something like the following happen.var a = ["john", "April", "tim", "Zoe"];
a = a.sort();
// a is now ["April", "Zoe", "john", "tim"]
Using MooTools you add a method to the Array object which will allow you to sort while being insensitive to case.Array.implement({
insensitiveSort: function() {
var result = [], tmp = [], tmpH = $H();
this.each(function(val) {
tmp.push(val.toString().toLowerCase());
tmpH.include(val, val.toString().toLowerCase());
});
tmp = tmp.sort();
tmp.each(function(val) {
result.push(tmpH.keyOf(val));
});
return result;
}
});
Now you can do the following.var a = ["john", "April", "tim", "Zoe"];
a = a.insensitiveSort();
// a is now ["April", "john", "tim", "Zoe"]
-
Array.insensitiveSort in MooTools
2 Comments Posted on October 9th, 2009 Updated on October 9th, 2009
2 Comments
Leave a Comment
-
You
Sep 7, 2010
http://mooshell.net/vmfQL/