基于 SplPriorityQueue 实现的排序方法 #3313
limingxinleo
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
limingxinleo/hyperf-utils
前言
之前我们在进行数组排序时,经常使用
usort
ksort
等方法,但当我们排序规则复杂一些后,这些方法使用起来就不是那么的方便了。后来发现了
\SplPriorityQueue
,进行简单的封装后,就更加方便使用了。基础封装
Laminas\Stdlib\SplPriorityQueue
是对\SplPriorityQueue
的封装,更加方便使用。上述代码,可以看到,我们额外增加了参数
$serial
,这个字段会让$priority
一致的元素可以按照优先插入元素的顺序排序。代码测试
我们只需要调用
sort
方法,然后在后面的匿名函数中返回对应元素的权重即可。Beta Was this translation helpful? Give feedback.
All reactions