In php how does usort() function works

后端 未结 5 762
暖寄归人
暖寄归人 2020-12-01 05:54

I have looked at the php documentation, tutorials online and none of them how usort is actually working. I have an example i was playing with below.

$data =          


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-01 06:11

    The callback provided to the sorting functions in PHP have three return values:

    0:  both elements are the same
    -1 (<0): the first element is smaller than the second
    1 (>0):  the first element is greater
    

    Now, usort probably uses some kind of quicksort or mergesort internally. For each comparison it calls your callback with two elements and then decides if it needs to swap them or not.

提交回复
热议问题