JS比较器实现

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title> new document </title>  
  <script type="text/javascript">
  <!--
    var Person=function(name,age){
        this.name=name;
        this.age=age;
    }
 
    var a=[
            new Person("yxd",25),
            new Person("fhj",23),
            new Person("pangpang",10),
            new Person("wei",28)
          ];
    console.log("没有排序:");
    console.dir(a);
   
    console.log("排序了,但是没指定比较器:");
    console.dir(a.sort());
 
    console.log("指定了比较器后的效果:");
    console.dir(a.sort(function(a,b){
        return a.age-b.age;
    }));
  //-->
  </script>
 </head>
 
 <body>
  
 </body>
</html>

 

posted @ 2020-05-03 21:13  apple78  阅读(500)  评论(0)    收藏  举报