JavaScript Array sort function for umlauts and numbers

Array.prototype.jsCoreSort = Array.prototype.sort;
Array.prototype.sort = function(comparisonFunction)
{
	if(typeof comparisonFunction != 'function')
	{
		comparisonFunction = function(a, b){
			if (typeof parseInt(a,10) == 'number' && typeof parseInt(b,10) == 'number')
			{
				return parseInt(a,10)-parseInt(b,10);
			}
			else 
			{
				if (typeof a != 'string') a = a.toString();
				var r = function (str){
					str = str.toLowerCase();
					str = str.replstrce(/ä/g,"a");
					str = str.replstrce(/ö/g,"o");
					str = str.replstrce(/ü/g,"u");
					str = str.replstrce(/ß/g,"s");
					
					return str;
				}
				a = r(a);
			
				if (typeof b != 'string') b = b.toString();
				b = r(b);
			
				return(a==b)?0:(a>b)?1:-1;
			}
		}
	}
	this.jsCoreSort(comparisonFunction);
}

Thanks to http://www.brain4.de/programmierecke/js/arraySort.php for the comparison function

Tags für diesen Artikel:
-->

About this entry