Just put the code in less nonsense:
<body> <div> sort() sorts the array, does not open up new memory, and replaces the original array elements</div> <div id="showBox"> 1. Simple array simple sort<script type="text/javascript"> var arrSimple=new Array(1,8,7,6); arrSimple.sort(); document.writeln(arrSimple.join()); </script> </div> <div> 2. Simple array custom sort<script type="text/javascript"> var arrSimple2=new Array(1,8,7,6); arrSimple2.sort(function(a,b){ return ba}); document.writeln(arrSimple2.join()); </script> Explanation: a,b represents any two elements in the array, if return > 0 b before a; reutrn < 0 a before b; when a=b, browser compatibility is simplified: ab output is sorted from small to large, and ba output is sorted from large to small. </div> <div> 3. Simple object List custom attribute sorting<script type="text/javascript"> var objectList = new Array(); function Persion(name,age){ this.name=name; this.age=age; } objectList.push(new Persion('jack',20)); objectList.push(new Persion('tony',25)); objectList.push(new Persion('stone',26)); objectList.push(new Persion('mandy',23)); //Sort objectList.sort(function(a,b){ return a.age-b.age}); for(var i=0;i<objectList.length;i++){ document.writeln('<br />age:'+objectList[i].age+' name:'+objectList[i].name); } </script> </div> <div> 4. Sort of editable attributes by simple object List<script type="text/javascript"> var objectList2 = new Array(); function WorkMate(name,age){ this.name=name; var _age=age; this.age=function(){ if(!arguments) { _age=arguments[0];} else { return _age;} } } objectList2.push(new WorkMate('jack',20)); objectList2.push(new WorkMate('tony',25)); objectList2.push(new WorkMate('stone',26)); objectList2.push(new WorkMate('mandy',23)); //Sort by age from childhood to large objectList2.sort(function(a,b){ return a.age()-b.age(); }); for(var i=0;i<objectList2.length;i++){ document.writeln('<br />age:'+objectList2[i].age()+' name:'+objectList2[i].name); } </script> </div></body>The above article in-depth understanding of the sort sort of js array is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.