search

Home  >  Q&A  >  body text

I don’t understand the sorting of array custom functions. Can any expert help me with the answer? Thank you.

QQ图片20180828002804.png

Who can tell me what the values ​​of these two parameters are and where they come from. Why can this function be sorted when written like this? I have been thinking about it all night and I don’t understand.

漠漠漠漠2312 days ago1367

reply all(10)I'll reply

  • YHF

    YHF2018-08-30 15:59:49

    $var1 and $var2 are two adjacent key values ​​​​in the $arr array. Each time, the two adjacent key values ​​​​are compared, that is, key value 1 is first compared with key value 2, and after the comparison, the key value is 2 is compared with key value 3, and so on, all the way to the end of the array.

    reply
    0
  • 漠漠

    Every time a comparison is made, it always returns 1 or -1 or 0, right? So how did he get this sequence in the end?

    漠漠 · 2018-08-30 16:06:53
    YHF

    In descending order, 1 means greater than, -1 means less than, 0 means equal to. If 1 means that the current two $var1 are larger than $var2, $var1 will be at the front, and $var2 will be at the back. Each time the two are next to each other. After a comparison, you can finally get the result. If you want to get ascending order, you only need to swap -1 and 1 in the return value to get ascending sorting.

    YHF · 2018-08-30 16:22:46
  • 秋香姐家的小书童

    秋香姐家的小书童2018-08-28 16:38:43

    This is just a built-in function.

    reply
    0
  • 漠漠

    What do you mean? We just need to do it according to this method. We don’t need to know how to calculate it, right?

    漠漠 · 2018-08-28 17:22:11
  • 墨尘致远

    墨尘致远2018-08-28 11:50:04

    usort is a built-in function in PHP. usort() allows users to sort arrays using custom functions.

    usort compares two elements each time and obtains the return value for sorting. 0, -1, and 1 represent equal, less than, and greater respectively.

    reply
    0
  • 漠漠

    Is it similar to bubble sort?

    漠漠 · 2018-08-28 15:16:37
  • White secretary

    White secretary2018-08-28 11:29:47

        

    <?php 
    echo "我可以和你们成为好朋友吗 哭唧唧";
    >


    reply
    0
  • 漠漠

    are you laughing at me

    漠漠 · 2018-08-28 15:12:13
    White secretary

    I'm... just joking... I'm new to programming... Brother... don't be angry... QAQ

    White secretary · 2018-08-28 16:22:59
    漠漠

    Haha, I’m new to this too

    漠漠 · 2018-08-28 16:25:35
  • Cancelreply