如何使用usort在php中执行自然排序
问题描述:
有人知道在对象上使用PHP中的usort函数执行自然顺序排序的功能是什么.
Does anyone know what the function is to perform a natural order sort using the usort function in PHP on an object.
让我们说对象($ obj-> Rate)在
Lets say the object ($obj->Rate)has a range of values in
$obj->10
$obj->1
$obj->2
$obj->20
$obj->22
我要如何使sort函数返回
What is I am trying to get the sort function to return
$obj->22
$obj->20
$obj->10
$obj->2
$obj->1
作为我当前的标准排序功能
As my current standard sort function
function MySort($a, $b)
{
if ($a->Rate == $b->Rate)
{
return 0;
}
return ($a->Rate < $b->Rate) ? -1 : 1;
}
正在返回
$obj->1
$obj->10
$obj->2
$obj->20
$obj->22
答
将 strnatcmp 用于您的比较功能.例如就这么简单
Use strnatcmp for your comparison function. e.g. it's as simple as
function mysort($a, $b) {
return strnatcmp($a->rate, $b->rate);
}