Search code examples
phparrayssortingquicksortspl

How to sort multikey in php?


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;
 });

Solution

  • 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);
    });