如何在PHP中排序多键?

时间:2016-02-08 08:33:52

标签: php arrays sorting quicksort spl

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

这是我的数组输入赞

 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

我想输出

 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

如何在php中对多键进行排序? 我是Php的初学者。我在PHP中使用以下代码但输出不会正确。如果按日期或月份排序中的任何一个,输出将正确。否则(日期或月份),输出将无法正确显示。 Plz帮助任何解决方案。

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

1 个答案:

答案 0 :(得分:1)

以下代码完成了这项工作。 strtotime将文本日期解析为Unix时间戳。

Option Explicit

Public Const equal As String = "="
Public Const dot = "."

Sub GetTableNames()
    Dim source As Worksheet
    Set source = Worksheets("source")

    Dim stringTable As Range
    Set stringTable = source.Range("A1:A5")

    stringTable.Replace What:=equal, Replacement:=dot, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

    Dim checkDuplicity As Collection
    Dim WrdArray As Variant
    Dim stringTableItem As Variant
    Dim part As Variant
    Dim strg As String

    Set checkDuplicity = New Collection

    For Each stringTableItem In stringTable.Value
        WrdArray = Split(stringTableItem, dot)
        For Each part In WrdArray
            part = Trim(part)
            If LCase(part) Like "table*" Then

                On Error Resume Next

                checkDuplicity.Add part, part
                If Err.Number = 0 Then _
                    strg = strg & vbNewLine & part

                On Error GoTo 0

            End If

        Next part
    Next stringTableItem

    MsgBox strg

End Sub