How to have a stable sort in PHP with arsort()?

后端 未结 7 719
迷失自我
迷失自我 2020-12-03 22:40

i need to sort an array in php based on value, array use some numbers for keys and values, for example like this:

$a = array(70 => 1 ,82          


        
7条回答
  •  情书的邮戳
    2020-12-03 23:18

    PHP use quicksort as base sorting-algorithms. You hae two options:

    1. first Uses usort and define a compare-function, which respect the former order
    2. programm your own stable sorting-mechanism

    I have programmed a package porth/avalanchesort (https://github.com/porthd/avalanchesort) on github, which contains a recursive naturell-merge-sort. You can use your own comparsionfunction and your own data-structure. The package contains an example for associative arrays and for list-arrays.

提交回复
热议问题