1

If I var_export I get

array (
  0 => 
  array (
    'date' => 2017,
    'id' => 128343,
  ),
  1 => 
  array (
    'date' => 1976,
    'id' => 128315,
  ),
  2 => 
  array (
    'date' => 2006,
    'id' => 128310,
  ),
  3 => 
  array (
    'date' => 1967,
    'id' => 128304,
  ),
  4 => 
  array (
    'date' => 1938,
    'id' => 128295,
  ),
  5 => 
  array (
    'date' => 1978,
    'id' => 128293,
  ),
  6 => 
  array (
    'date' => 1997,
    'id' => 128157,
  ),
  7 => 
  array (
    'date' => 2000,
    'id' => 128124,
  ),

The dates are mixed up. I am trying to sort those dates and keep the Ids attached to each date DESC in order then to loop again but ordered. I tried

function custom_sort_dt($a, $b) {
   return $a['date'] - $b['date'];
}
usort($dateOrdered, "custom_sort_dt");

But I still get wrong order.

// the following gives a date
$myDate = (int)get_post_meta($id, 'usp-custom-14', true);

// this attaches the Id and the date
$dateOrdered[] = array("date"=>$myDate, "id"=>$id);
1
  • @Emma updated the question Commented Feb 7, 2019 at 2:51

2 Answers 2

3

You could use array_multisort.

array_multisort($data, SORT_DESC, array_column($data, 'date'));
var_dump($data);

Note that this function operates on the original array. If you want a copy instead, build one before by

$copy = $data;
Sign up to request clarification or add additional context in comments.

1 Comment

@Emma See edit, multisort even takes the SORT_DESC constant.
0

Perhaps try reversing the comparison to sort in descending order:

return $b['date'] - $a['date'];

3 Comments

but look at the var export, the dates are mixed up, so it isn't about reverting the order
Interesting rob.m. Can you include the code that sorts and dumps the array?
Your question mentions var_export before usort. It doesn't seem you're doing that in the right order.

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.