-1
Array ( [0] => stdClass Object (    [Id] => 18 
                                [AccNo] => 1 
                                [Title] => Hardware 
                                [Description] => Mobile. 
                                [ManuDate] => 8th July 1942 
                                [MusCat] => Album 
                                [month] => 7 
                                [date] => 8 ) 
     [1] => stdClass Object (   [Id] => 20 
                                [AccNo] => 2 
                                [Title] => Food 
                                [Description] => Apple. 
                                [ManuDate] => 27th July 1942 
                                [MusCat] => Album 
                                [month] => 7 
                                [date] => 27 )
     [2] => stdClass Object (   [Id] => 24 
                                [AccNo] => 3 
                                [Title] => Hardware 
                                [Description] => Computer. 
                                [ManuDate] => 2nd July 1942 
                                [MusCat] => Album 
                                [month] => 7 
                                [date] => 2 )
     [3] => stdClass Object (   [Id] => 56 
                                [AccNo] => 4 
                                [Title] => Hardware 
                                [Description] => Printer 
                                [ManuDate] => 1942 
                                [MusCat] => Album 
                                [month] => 
                                [date] => 0 ) 
       [4] => stdClass Object ( [Id] => 105 
                                [AccNo] => 5 
                                [Title] => Object 
                                [Description] => Chair. 
                                [ManuDate] => 1942 
                                [MusCat] => Album 
                                [month] => 
                                [date] => 0 ) ) 

This is my array input Like

 Id Date                Title               Description

    0   8th July 1942       Hardware        Mobile
    1   27th August 1942    Food            Apple
    2   2nd July 1942       Hardware        Computer
    3   1942                Hardware        Printer
    4   1942                Object          Chair

I want output like

 Id Date                Title               Description
************************************************************
3   1942                Hardware             Printer
4   1942                Object               Chair
2   2nd July 1942       Hardware             Computer
0   8th July 1942       Hardware             Mobile
1   27th August 1942    Food                 Apple

How to sort multikey in php? I am beginner in Php. I am using following code in php but out put will not correctly. If any one of datewise or monthwise sort, Output will come correctly. otherwise both (datewise or monthwise), Output will not come correctly. Plz help any solution.

usort($value['year'], function ($a, $b) {
     if ($a->date == $b->date) return 0;                    
    return $a->date < $b->date ? -1 : 1;
});     

usort($value['year'], function ($a, $b) {
    if ($a->month == $b->month) return 0;
    return $a->month < $b->month ? -1 : 1;
 });
Nitin Karale
  • 786
  • 3
  • 10
  • 33
  • Instead of sorting it within PHP you can simply use `order by` clause within MySQL query – Ali Feb 08 '16 at 08:53
  • year does not seem like date field in DB, wouldn't be easier to set is as Date field and then `order by` in mysql? If you want still do it in php year is not valid datetime so you would need to check if this field is year and convert it to date then compare. – Robert Feb 08 '16 at 08:55
  • date should have year or date. I have to sort month wise and also day wise. It is not possible to sort by date in query – Nitin Karale Feb 08 '16 at 09:14

1 Answers1

1

The following code does the job. strtotime parses text date into a Unix timestamp.

usort($array, function($v1, $v2) {
    $d1 = strlen($v1->ManuDate) === 4 ? '01-01-' . $v1->ManuDate : $v1->ManuDate;
    $d2 = strlen($v2->ManuDate) === 4 ? '01-01-' . $v2->ManuDate : $v2->ManuDate;
    return strtotime($d1) - strtotime($d2);
});
Ihor Burlachenko
  • 3,657
  • 22
  • 23
  • Thanks for reference. I got solution. I converted manudate as date format like 1942-07-02 or 1942-01-01 or 1942-08-27. I add tempyear key attribute & used following function function date_compare($a, $b) { $t1 = strtotime($a->tempYear); $t2 = strtotime($b->tempYear); return $t1 - $t2; } usort(#array, 'date_compare'); – Nitin Karale Feb 09 '16 at 05:35