根据日期对文本按字母顺序排序

时间:2019-08-07 19:45:29

标签: php arrays

我有一个数组。该数组包含在日期格式中创建的时间,以及文本格式中的名字。我必须先对“日期”进行排序,然后再对“名字”按字母顺序进行排序。如果重复相同的日期,名字将按字母顺序排序。

例如如果名字F&C是相同的日期,那么订单将是C&F。

我尝试了此功能进行排序。

 function compareByName($a, $b) {
    if($a['timecreated'] == $b['timecreated'])
    {
    return strcmp($b['firstname'], $a['firstname']);
    }
    else{
    return $a['timecreated'] < $b['timecreated']; 
    }
}
    usort($a, 'compareByName');

这是代码:

<?php
$a = Array
(
    0 => array
        (
            "id" => 5305,
            'firstname' => "testb",
            "timecreated" => "08/07/2019"
        ),

    1 => array
        (
            "id" => 5304,
            'firstname' => "testa",
            "timecreated" => "08/07/2019"
        ),
2 => array
        (
            "id"=> 122,
            'firstname' => "John",
            "timecreated" => "01/14/2019"
        ),

3 => array
        (
            "id" => 85,
            'firstname' =>"Josh",
            "timecreated" => "01/07/2019"
        ),


4 => array
        (
            "id" => 62,
            'firstname' => "CAN",
            "timecreated" => "11/05/2018"
        ),

5 => array
        (
            "id" => 61,
            'firstname' => "CAB",
            "timecreated" => "11/05/2018"
        ),

6 => array
        (
            "id" => 59,
            'firstname' => "CAD",
            "timecreated" => "11/02/2018"
        ),

7 => array
        (
            "id" => 58,
            'firstname' => "CAR",
            "timecreated" => "11/01/2018"
        )

);


function compareByName($a, $b) {
    if($a['timecreated'] == $b['timecreated'])
    {
    return strcmp($b['firstname'], $a['firstname']);
    }
    else{
    return $a['timecreated'] < $b['timecreated']; 
    }
}
    usort($a, 'compareByName');
print_r($a);


我的预期结果:

Array
(
    [0] => Array
        (
            [id] => 5304
            [firstname] => testa
            [timecreated] => 08/07/2019
        )

    [1] => Array
        (
            [id] => 5305
            [firstname] => testb
            [timecreated] => 08/07/2019
        )

    [2] => Array
        (
            [id] => 122
            [firstname] => John
            [timecreated] => 01/14/2019
        )

    [3] => Array
        (
            [id] => 85
            [firstname] => Josh
            [timecreated] => 01/07/2019
        )

    [4] => Array
        (
            [id] => 61
            [firstname] => CAB
            [timecreated] => 11/05/2018
        )

    [5] => Array
        (
            [id] => 62
            [firstname] => CAN
            [timecreated] => 11/05/2018
        )

    [6] => Array
        (
            [id] => 59
            [firstname] => CAD
            [timecreated] => 11/02/2018
        )

    [7] => Array
        (
            [id] => 58
            [firstname] => CAR
            [timecreated] => 11/01/2018
        )

)

我的结果不正确。请帮助,谢谢。

2 个答案:

答案 0 :(得分:1)

您正在将日期作为字符串进行比较,您需要首先将它们变成DateTime对象。另外,为了获得您的结果,我必须更改字符串比较顺序:

function compareByName($a, $b) {
    $time_a = new DateTimeImmutable($a['timecreated']);
    $time_b = new DateTimeImmutable($b['timecreated']);
    if($time_a == $time_b)
    {
        return strcmp($a['firstname'], $b['firstname']);
    }        
    return $time_a < $time_b;         
}

Demo

答案 1 :(得分:0)

如果使用特殊的PHP类,这非常容易。

//class: https://github.com/jspit-de/tableArray
$assorted = tableArray::create($a)
  ->orderBy('DATEFORMAT("Y-m-d",timecreated) DESC,firstname ASC')
  ->fetchAll()
;