2012-05-14 32 views
1

问题如何对以下php数组进行排序?

Array ([0] => 12w12 [1] => 13w12 [2] => 14w12 [3] => 15w12 [4] => 2w13 [5] => 3w13 [6] => 4w13 [7] => 3w12 [8] => 7w12 [9] => 9w12) 

答案应该是

Array ([0] => 3w12 [1] => 7w12 [2] => 9w12 [3] => 12w12 [4] => 13w12 [5] => 14w12 [6] => 15w12 [7] => 2w13 [8] => 3w13 [9] =>4w13 ) 
+1

什么是2w13,它是怎么来的__after__ 15w12? – Hindol

+0

先按第二个“字段”排序? –

+0

提示:你只需要写一个比较函数 –

回答

-1

做php.net有点搜索usort,让自己的排序方法,你想怎么实现似乎并不标准排序在全部

+4

-1,这应该是一个评论 –

1
function cmp($a, $b){ 
    if ($a == $b) { return 0; } 

    list($first1, $last1) = explode("w", $a); 
    list($first2, $last2) = explode("w", $b); 

    return (($last1.$first1) < ($last2.$first2)) ? -1 : 1; 
} 

usort($array, "cmp");