zoukankan      html  css  js  c++  java
  • js参考---数组排序函数sort

    js参考---数组排序函数sort

    一、总结

    一句话总结:

    sort的作用是排序数组,@param:compareFn:The name of the function used to determine the order of the elements. If omitted, the elements are sorted in ascending, ASCII character order.

    1、如果sort没有传递回调函数作为参数,那么sort的排序规则是什么?

    Unicode的方式排序,一个字符一个字符的比

    2、关于sort函数中参数:回调函数的参数a、b的位置关系?

    a在b前:浏览器将会分别使用数组中的元素作为实参去调用回调函数,使用哪个元素调用不确定,但是肯定的是在数组中a一定在b前边

    3、sort函数会根据回调函数的什么来交互元素的值?

    浏览器会根据回调函数的返回值来决定元素的顺序,大于0交互位置,小于0位置不变,等于0表示相等,位置也不变

    4、sort函数的参数对应的回调函数返回a-b表示升序还是降序?

    表示升序,因为返回值大于0交互位置,a-b大于0则a大于b,交互位置之后自然是升序,a-b小于零则不交互位置

    二、数组排序函数sort

    博客对应课程的视频位置:

     1 <!DOCTYPE html>
     2 <html>
     3     <head>
     4         <meta charset="utf-8" />
     5         <title></title>
     6         <script type="text/javascript">
     7             
     8             arr = ["b","d","e","a","c"];
     9             
    10             /*
    11              * sort()
    12              *     - 可以用来对数组中的元素进行排序
    13              *     - 也会影响原数组,默认会按照Unicode编码进行排序
    14              */
    15             arr.sort();
    16             //arr.reverse();
    17             
    18             /*
    19              * 即使对于纯数字的数组,使用sort()排序时,也会按照Unicode编码来排序,
    20              *     所以对数字进排序时,可能会得到错误的结果。
    21              * 
    22              * 我们可以自己来指定排序的规则
    23              *     我们可以在sort()添加一个回调函数,来指定排序规则,
    24              *         回调函数中需要定义两个形参,
    25              *         浏览器将会分别使用数组中的元素作为实参去调用回调函数
    26              *         使用哪个元素调用不确定,但是肯定的是在数组中a一定在b前边
    27              *     - 浏览器会根据回调函数的返回值来决定元素的顺序,
    28              *         如果返回一个大于0的值,则元素会交换位置
    29              *         如果返回一个小于0的值,则元素位置不变
    30              *         如果返回一个0,则认为两个元素相等,也不交换位置
    31              * 
    32              *     - 如果需要升序排列,则返回 a-b
    33              *         如果需要降序排列,则返回b-a
    34              */
    35             arr = [5,4,2,1,3,6,8,7];
    36             
    37             arr.sort(function(a,b){
    38                 
    39                 //前边的大
    40                 /*if(a > b){
    41                     return -1;
    42                 }else if(a < b){
    43                     return 1;
    44                 }else{
    45                     return 0;
    46                 }*/
    47                 
    48                 //升序排列
    49                 //return a - b;
    50                 
    51                 //降序排列
    52                 return b - a;
    53                 
    54             });
    55             
    56             console.log(arr);
    57             
    58         </script>
    59     </head>
    60     <body>
    61         
    62     </body>
    63 </html>
     
  • 相关阅读:
    BIOS/MBR UEFI/GPT关系与区别-资料整理
    raid 简单了解
    MBR主引导记录
    linux 安装vscode
    chrome 获得点击按钮时的事件
    python计算纪念日相关
    python error: curl: (1) Protocol "'https" not supported or disabled in libcurl
    linux go with vscode
    postman 进阶技巧
    mysql常用时间函数与类型转换
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/12517412.html
Copyright © 2011-2022 走看看