`
blueion
  • 浏览: 39643 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

自定义的排序方法

阅读更多

一般情况下JS的排序利用Array的sort方法:

 

 

arrayobj.sort(sortfunction) 
 

针对一些特殊的排序要求,还可以自定义sortfunction

 

 

 

如果为 sortfunction 参数提供了一个函数,那么该函数必须返回下列值之一: 

负值,如果所传递的第一个参数比第二个参数小。 
零,如果两个参数相等。 
正值,如果第一个参数比第二个参数大。 
 

这里提供一个按字母顺序排序的方法,由于某个字母大小写的ASCII码不同,所以不能用'<','>'比较。因此用一个Object来定义字符顺序,注意顺序完全按照自己的需求定义:

 

var character = {
                0: -2,
		1: -1,
		2: 2,
		3: 3,
		4: 4,
		5: 5,
		6: 6,
		7: 7,
		8: 8,
		9: 9,
				
                a: 11,
                A: 12,
                b: 13,
                B: 14,
                c: 15,
                C: 16,
                d: 17,
                D: 18,
                e: 19,
                E: 20,
                f: 21,
                F: 22,
                g: 23,
                G: 24,
                h: 25,
                H: 26,
                i: 27,
                I: 28,
                j: 29,
                J: 30,
                k: 31,
                K: 32,
                l: 33,
                L: 34,
                m: 35,
                M: 36,
                n: 37,
                N: 38,
                o: 39,
                O: 40,
                p: 41,
                P: 42,
                q: 43,
                Q: 44,
                r: 45,
                R: 46,
                s: 47,
                S: 48,
                t: 49,
                T: 50,
                u: 51,
                U: 52,
                v: 53,
                V: 54,
                w: 55,
                W: 56,
                x: 57,
                X: 58,
                y: 59,
                Y: 60,
                z: 61,
                Z: 62
            };
 

在这个基础上,构建判断两个单词的大小的sortfunction:

 

 

 function compare(str1, str2,/*Boolen, compare with chinese?*/isChinese){
                console.info('compare');
                str1 = dojo.trim(str1);
                str2 = dojo.trim(str2);
                i = 0; 
		var s1 = str1.charAt(i);
           	var s2 = str2.charAt(i++);			
				
                while (s1) {
//					if(isChinese){
//						 
//						console.info('in chinese:')
//						if( s1>= 0x4E00 && s1 <= 0x9FA5){
//							alert('s1 chinese!')
//							//s1,s2 both chinese,compare ASCII
//							if(s2 >= 0x4E00 && s2 <= 0x9FA5){
//								alert('s2 chinese!')
//								if(s1<s2){
//									return -1;
//								}else if(s1>s2){
//									return 1
//								}else{
//									continue;
//								}
//							}else{
//								//s2 is not chinese,s1 is bigger
//								return 1;
//							}
//						}else if(s2 >= 0x4E00 && s2 <= 0x9FA5){
//							//s2 is chinese,s1 isn't chinese, s2 is bigger
//							alert('s2 chinese!')
//							return -1;
//						}
//						//none of s1,s2 is chinese,to normal process 
//					}
 					if(!s2){
						return 1;
					}
					//special character sort by ASCII order
               		if(!character[s1]){
						if(!character[s2]){
							if(s1<s2){
								return -1;
							}else if(s1>s2){
								return 1;
							}else{
								s1 = str1.charAt(i);
                            s2 = str2.charAt(i++);
								continue;
							}
						}else{
							return -1;
						}
					}else if(!character[s2]){
						return 1;
					}
					
					//alphabet and number
                    if (character[s1] < character[s2]) {
                        return -1;
                    }
                    else 
                        if (character[s1] > character[s2]) {
                            return 1;
                        }
                        else {
                            s1 = str1.charAt(i);
                            s2 = str2.charAt(i++);
                        }
                }
				if(!s2){
					return 0;
				}
                return -1;
            }

 

当然这个方法不一定只用于Array的sort中,也可以在自定义数据结构中使用,比如linklist,double linklist..

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics