1

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  ) 
4
  • 1
    What is 2w13 and how come it comes after 15w12? Commented May 14, 2012 at 11:20
  • ordered by second "field" first ? Commented May 14, 2012 at 11:21
  • hint: you just have to write a comparison function Commented May 14, 2012 at 11:22
  • 2w13 is 2nd week of 2013 and 15w12 is 15th week of 2012 that's why is comes after 15w12 Commented May 15, 2012 at 6:39

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.