user1393576
user1393576

Reputation: 11

how to sort the following php array?

Question

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

Answer should be

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

Upvotes: 1

Views: 86

Answers (3)

472084
472084

Reputation: 17885

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");

Upvotes: 1

Hindol
Hindol

Reputation: 2990

You can use PHP usort and write your own comparison function.

Upvotes: 1

malko
malko

Reputation: 2382

do a bit search on php.net for usort and make your own sorting method as what you are trying to achieve don't seem standard ordering at all

Upvotes: -1

Related Questions