how php array_multisort work?

前端 未结 3 1609
死守一世寂寞
死守一世寂寞 2020-12-10 18:30

i have some problem to understand array_multisort

See how it sorts when two values are the same:

 $a1=array(\"Dog\",\"Dog\",\"Cat\");
 $a2=array(\"P         


        
3条回答
  •  生来不讨喜
    2020-12-10 19:25

    Well, you're sorting the arrays in a similar way to programs like Excel. Each array corresponds to a column.

    First, all arrays are sorted by the first array given. If there are identical values, those affected are sorted by the second array given. If there are again equal values, the third array is used, etc.

    Or in other words: The arrays are sorted using all arrays, but beginning on the right (if you assume it really sorts by all columns once).

    For your particular example (the second one):

    At first you want to sort in ascending order, so Cat will be first. Therefore the last array element will be moved to the first position in both arrays. The other two elements, Dog are equal. This causes the function to look at the next array. It's told to sort this array in descending order, so Pluto comes first. In this case this leads to the result that the elements aren't moved at all (as their order is correct already).

提交回复
热议问题