-2

I want to sort this object of arrays in php. first by date and than by hour. the problem is the object is arrays of arrays, and I want the double sort, first by date and then by the hour. the sort func is just for one value. can someone please advise?

 array (
  '621223d55f426' => 
  array (
    'Timestamp' => '2022-02-20 04:00',
    'Date' => '2022-02-20',
    'Hour' => '04',
  ),
  '621223d55f438' => 
  array (
    'Timestamp' => '2022-02-20 00:00',
    'Date' => '2022-02-20',
    'Hour' => '00',
  ),
  '621223d55f43f' => 
  array (
    'Timestamp' => '2022-02-20 01:00',
    'Date' => '2022-02-20',
    'Hour' => '01',
  ),
  '621223d55f444' => 
  array (
    'Timestamp' => '2022-02-20 03:00',
    'Date' => '2022-02-20',
    'Hour' => '03',
  ),
  '621223d55f449' => 
  array (
    'Timestamp' => '2022-02-20 02:00',
    'Date' => '2022-02-20',
    'Hour' => '02',

  ),
)

This is the expected data that I need to be ordered:

array (
  '621223d55f438' => 
  array (
    'Timestamp' => '2022-02-20 00:00',
    'Date' => '2022-02-20',
    'Hour' => '00',
  ),
  
   '621223d55f43f' => 
  array (
    'Timestamp' => '2022-02-20 01:00',
    'Date' => '2022-02-20',
    'Hour' => '01',
  ),
  
    '621223d55f449' => 
  array (
    'Timestamp' => '2022-02-20 02:00',
    'Date' => '2022-02-20',
    'Hour' => '02',

  ),
  
    '621223d55f444' => 
  array (
    'Timestamp' => '2022-02-20 03:00',
    'Date' => '2022-02-20',
    'Hour' => '03',
  ),

  '621223d55f426' => 
  array (
    'Timestamp' => '2022-02-20 04:00',
    'Date' => '2022-02-20',
    'Hour' => '04',
  ),

 


)
Bastian
  • 1,089
  • 7
  • 25
  • 74
  • You asked this already 12 hours ago (https://stackoverflow.com/q/71198066/1427878), and that was also already closed as a duplicate of the general explanation on how to sort arrays in PHP. – CBroe Feb 21 '22 at 08:06
  • sorry but this is not answered the question. if I want to sort by two parameters and not one. I know the sort finc – Bastian Feb 21 '22 at 08:07
  • It _is_ answered in the duplicate, once you realize that writing the comparison function for a `usort` call, is your responsibility. So write one that compares by your first criterion first, and by your second one after, if necessary. (Or, realize that you do not even need to sort by two criteria here - your `Timestamp` value is in a "sortable" format, that can be compared by simple string comparison.) – CBroe Feb 21 '22 at 08:09
  • You might want to consider [uasort()](https://www.php.net/uasort) if you want to maintain index association, that is, keep the same key with the same array. – KIKO Software Feb 21 '22 at 08:13
  • No I want to sort by date not by time stamp. I want to sort by date, and than in the same date to sort by hour, so the results if first all the dates that are today and than all the times, and then yesterday and the hours of the yesterday – Bastian Feb 21 '22 at 08:13
  • Perhaps it would be helpful if you gave an example, in your question. As far as I can tell CBroe is right, sorting by date and then hour is the same as sorting by the timestamp. – KIKO Software Feb 21 '22 at 08:16
  • The Timestamp _is_ the combination of Date and Hour here, basically. So whether you would sort by date first and then by hour second, or by this combined timestamp value directly - would make no difference for the result. Can you please show what exact result you want to get, for the above input data? If have a slight suspicion that you might not actually be talking about _sorting_ to begin with, but perhaps want to re-group the data or something ...? – CBroe Feb 21 '22 at 08:16
  • I want it to rearranged or sort, by the time stamp – Bastian Feb 21 '22 at 09:09
  • You are still not making this clear. Again: Can you _please_ show what exact result you want to get, for the above input data? – CBroe Feb 21 '22 at 10:51
  • I added the expected results – Bastian Feb 21 '22 at 10:57
  • Given your expected result it would be perfectly sortable only on the `Timestamp`. No double sorting needed. – KIKO Software Feb 21 '22 at 11:02

1 Answers1

0

The solution to this problem has already been shown here several times. For example here: sorting with uasort (multiple conditions).

uasort($array,function($a,$b){
  return $a['Date'] <=> $b['Date']  //first order
    ?:   $a['Hour'] <=> $b['Hour']  //second order
  ;
});
jspit
  • 7,276
  • 1
  • 9
  • 17