PHP's USORT Callback Function Parameters

后端 未结 3 1784
离开以前
离开以前 2021-02-05 21:48

This is a really esoteric question, but I\'m genuinely curious. I\'m using usort for the first time today in years, and I\'m particularly interested in what exactly is going on.

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-05 22:10

    To sort anything you need a means to compare two items and figure out if one comes before the other. This is what you supply to usort. This function will be passed two items from your input array, and returns the order they should be in.

    Once you have a means to compare two elements, you can use sort-algorithm-of-your-choice.

    If you are unfamiliar, you might like to look at how a simple naive algorithm like bubblesort would use a comparison function.

    Behind the scenes, PHP is using a quicksort.

提交回复
热议问题