修复IE9-- & safari 的sort方法

解决方案其实就是冒泡排序的一个包装

本补丁不修复各浏览的排序算法不一致的问题

当前版本 v0.2
v0.1 修复IE9-- Array.prototype.sort 不能根据 对象属性 做排序的遗憾
v0.2 修复safari 不支持函数参数


;
!function(window){
	var ua = window.navigator.userAgent.toLowerCase(),
		reg = /msie|applewebkit.+safari/;
	if(reg.test(ua)){
		var _sort = Array.prototype.sort;
		Array.prototype.sort = function(fn){
			if(!!fn && typeof fn === 'function'){
				if(this.length < 2)	return this;

				var i = 0, j = i + 1, l = this.length, tmp, r = false, t = 0;
				
				for(; i < l; i++){
					for(j = i + 1; j < l; j++){
						t = fn.call(this, this[i], this[j]);
						
						r = (typeof t === 'number' ? t :
								!!t ? 1 : 0) > 0 
									? true : false;

						if(r){
							tmp = this[i];
							this[i] = this[j];
							this[j] = tmp;
						}
					}
				}

				return this;
			}else{
				return _sort.call(this);
			}
		};
	}
}(window);
posted @ 2011-07-13 15:20  Alucelx  阅读(1014)  评论(0编辑  收藏  举报