Nitin Karale February 2016

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        

Answers


Ihor Burlachenko February 2016

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

Post Status

Asked in February 2016
Viewed 2,124 times
Voted 13
Answered 1 times

Search




Leave an answer