So I'm not exactly sure if the title fits it best, but here's what the array looks like:
array (
[0] => array (
[category] => 'Value_1'
[date] => '01/01/2011'
[data] => 'A'
)
[1] => array (
[category] => 'Value_3'
[date] => '01/01/2000'
[data] => 'B'
)
[2] => array (
[category] => 'Value_2'
[date] => '01/01/2011'
[data] => 'D'
)
[3] => array (
[category] => 'Value_2'
[date] => '01/01/2010'
[data] => 'A'
)
[4] => array (
[category] => 'Value_2'
[date] => '01/01/2011'
[data] => 'C'
)
)
How I'd like this data to be sorted would be the following:
- Preserve the order of the categories
- Within the categories, order by date DESC
- If dates appear multiple times, order by data alphabetically ASC
The example array would then be sorted to array ([0], [1], [4], [2], [3])
, more specifically:
array (
[0] => array (
[category] => 'Value_1'
[date] => '01/01/2011'
[data] => 'A'
)
[1] => array (
[category] => 'Value_3'
[date] => '01/01/2000'
[data] => 'B'
)
[2] => array (
[category] => 'Value_2'
[date] => '01/01/2011'
[data] => 'C'
)
[3] => array (
[category] => 'Value_2'
[date] => '01/01/2011'
[data] => 'D'
)
[4] => array (
[category] => 'Value_2'
[date] => '01/01/2010'
[data] => 'A'
)
)
My issue is I know I'll need usort
and/or array_multisort()
, but I'm not sure exactly sure how to efficiently iterate through a loop in order to sort with the criteria I gave.