我有一个不寻常的问题。
我生成汽车模型列表(每个列表都是一个数组)。在大多数情况下,使用natcasesort()
对列表进行排序很好,但在某些情况下,列表的子集需要按照自定义顺序按时间顺序排序,而不是按字母顺序排序。
我需要的是拥有一个全局函数,检查每个列表并进行相应的排序。困扰我的部分是我只需要排序的列表的子集,并且每个其他元素都需要应用标准natcasesort()
。
我已经写过我认为好的基础,但我完全坚持排序算法本身:
function customSort($arr){
$groups=array(
// My custom sort groups, in desired final sort order
array('Porsche 911 (964/965)','Porsche 911 (993)','Porsche 911 (996)','Porsche 911 (997)','Porsche 911 (991)'),
array('BMW M2','BMW 3 Series','BMW M3','BMW 4 Series','BMW M4','BMW 5 Series','BMW M5','BMW 6 Series','BMW M6')
);
foreach($groups as $v){
// If match is found, sort and return array
if(count(array_intersect($arr,$v))>0){
usort($arr,'runSort');
return $arr;
}
}
// If match not found, run and return standard sort
natcasesort($arr);
return $arr;
}
function runSort($a,$b){
// ???
}
$oldarray=array('AAA','Porsche 911 (993)','ZZZ','Porsche 911 (991)','Porsche 911 (997)','hhh');
$newarray=customSort($oldarray);
在print_r
上执行$newarray
应返回:
> AAA
> hhh
> Porsche 911 (993)
> Porsche 911 (997)
> Porsche 911 (991)
> ZZZ
有什么建议吗?谢谢!