So I have a 2D array that looks a bit like this:
[0] => Array(
[date] => 23-01-2017
[name] => bbb
[othertext] => text2
)
[1] => Array(
[date] => 23-01-2017
[name] => aaa
[othertext] => text3
)
[2] => Array(
[date] => 24-01-2017
[name] => aaa
[othertext] => text1
)
NOTE: This question is not tagged as MySQL, the database used is MongoDB with the sort type of 'date' => 'asc'
.
Currently this is returned from my database sorted by date, but does not take into account the name property. I'd like to now sort this by date
, and for entries with the same date to then sort by name
.
My current approach is to run an array_multisort
on the data:
array_multisort(
$array['date'], SORT_STRING,
$array['name'], SORT_STRING,
$arrayCopy //<--This copy of the array has the full datetime object
);
But this then sorts the date as a string, so in certain scenarios it sorts it incorrectly; e.g. 1st March
would go before the 2nd Feb
. If putting the month first then again it sorts incorrectly when Dec/Jan dates are sorted.
What's the correct approach to this? I've seen mention of usort()
, but I'm unsure how to implement it in this use case. Does array_multisort
have functionality for dates?