Js,jquery the implementation of the Sort:
Important: the simplest way to achieve a sort is to
- First read the label into an array of objects with jquery
- Sort an array of objects with JS (sort on an array of objects, do not attempt to directly change the contents of the Page)
- Overwrites an array of labels on a Web page with Object array contents;
//sort from short to long in text varArr_a=NewArray (); varI=0; $(". type_list_txt > Span:contains (' T-shirt ')"). Parent (). children ("a"). each (function() {arr_a[i]=$( this). Clone (); I++; }); for(i=0; i<arr_a.length;i++){ for(j=i+1;j<arr_a.length;j++){ if($ (arr_a[i]). text (). length >$ (arr_a[j]). Text (). Length) {temp=arr_a[i]; arr_a[i]=arr_a[j]; arr_a[j]=temp; }}} I=0; $(". type_list_txt > Span:contains (' T-shirt ')"). Parent (). children ("a"). each (function(){ $( this). replacewith ($ (arr_a[i])); I++; });
Original Address: http://www.cnblogs.com/henw/archive/2011/04/26/2029474.html
Referring to the original text, I wrote my own sort, the original text is a simple selection of sort, my is bubble sort.
functionDoorder () {//#orderCom为页面上的按钮 varType = $ ("#orderCom"). attr ("type")); Console.log (type); varFlelems =NewArray (); varfi = 0; //GLOBALFL is a global variable and gets the elements individually$ ("#detail" +globalfl). Children (). each (function() {flelems[fi]= $( this). Clone (); Fi++; }); if(type== "asc"){ //ASC Sort for(vari=0;i<flelems.length-1;i++){ for(varj=0;j<flelems.length-i-1;j++){ if(parsefloat ($ (flelems[j]). attr (' cval ')) >parsefloat ($ (flelems[j+1]). attr (' cval ')))){ varEle =flelems[j]; flelems[j]= Flelems[j+1]; Flelems[j+1] =ele; } } } $("#orderCom"). attr ("type", "desc"); $("#orderCom"). text ("points ↓"); }Else{ //desc Sort for(vari=0;i<flelems.length-1;i++){ for(varj=0;j<flelems.length-i-1;j++){ if(parsefloat ($ (flelems[j]). attr (' cval ')) <parsefloat ($ (flelems[j+1]). attr (' cval ')))){ varEle =flelems[j]; flelems[j]= Flelems[j+1]; Flelems[j+1] =ele; } } } $("#orderCom"). attr ("type", "asc"); $("#orderCom"). text ("points ↑"); } fi= 0; //write the sorted HTML$ ("#detail" +globalfl). Children (). each (function(){ $( this). replacewith ($ (flelems[fi])); Fi++; });}
Go: implementation of Js,jquery sorting, implementation of page label sorting, label sorting