问答详情
源自:7-22 数组排序sort()

自定义排序返回值为1时,无效果

   var arr = ["国","处","科","处","科","处","科","部","国","司","处","部","国","部","司","处","司","处","司","处","科"];


   function arrSort(a,b){

    if( (a=="国" && ( b=="部" || b=="司" || b=="处" ||  b=="科")) ||

    (a=='部' && ( b=='司' || b=='处' ||  b=='科') ) ||

    (a=='司' && ( b=='处' ||  b=='科')) ||

    (a=='处' &&  b=='科' ) ){

    return -1;

    }

    if(a == b){

    return 0;

    }

   }

   document.write(arr.sort(arrSort));


提问者:朦胧5 2020-02-21 14:49

个回答

  • 慕姐8494960
    2020-05-07 17:26:49

    不太明白你的意思

  • 朦胧5
    2020-02-23 13:08:12

    已解决,我没考虑到的是,三种返回值都必须写出来,不然返回1时,是不会有效果。

            正确解法:

                    在else中加入返回值,记住在这种类型中自定义排序方法中三种返回值都要写出来