• 【js】多维排序


        var list = [];
            for(var i=0;i<100;i++){
                list.push({x:parseInt(Math.random()*5),y:parseInt(Math.random()*5),z:parseInt(Math.random()*5)})
            }
           
            console.info(list)
            mdSort(list,["x","y","z"])
            console.info(list)
            /*
                list 待排序集合,例如  [{x:1,y:2,z:3},{...}]  
                plist 排序的属性名,例如  ["x","y","z"]   
            */
            function mdSort(list,plist){
                if(list==null||list.length==0||plist==null||plist.length==0){
                    return;
                }
                sort(list,0,list.length-1,plist[0]);
                for(var i=1;i<plist.length;i++){
                    sort2(list,plist[i-1],plist[i])
                }
                function sort2(list,p1,p2){
                    var start=0;
                    for(var i=1;i<list.length;i++){
                        if(list[i][p1]!=list[i-1][p1]){
                            sort(list,start,i-1,p2)
                            start = i;
                        }
                        if(i==list.length-1){
                            sort(list,start,i,p2);
                        }
                    }
                }
                function sort(list,start,end,p){
                    if(end-start<1){
                        return;
                    }
                    var L=start;
                    var R=end;
                    var temp=list[start];
                    while(L!=R){
                        while(R>L){
                            if(list[R][p]<temp[p]){
                                list[L]=list[R]
                                break;
                            }
                            R--;
                        }
                        while(L<R){
                            if(list[L][p]>temp[p]){
                                list[R]=list[L]
                                break;
                            }
                            L++;
                        }
                    }
                    list[R]=temp;
                    sort(list,start,R-1,p);
                    sort(list,R+1,end,p);
                }
            }
  • 相关阅读:
    算法模板——线性欧拉函数
    2818: Gcd
    1688: [Usaco2005 Open]Disease Manangement 疾病管理
    3314: [Usaco2013 Nov]Crowded Cows
    3450: Tyvj1952 Easy
    1664: [Usaco2006 Open]County Fair Events 参加节日庆祝
    1054: [HAOI2008]移动玩具
    1432: [ZJOI2009]Function
    1121: [POI2008]激光发射器SZK
    1113: [Poi2008]海报PLA
  • 原文地址:https://www.cnblogs.com/LightChan/p/7955244.html
Copyright © 2020-2023  润新知