0

I have multidimensional array in PHP. Something like

$mylist = array(
    array('ID' => 1, 'title' => 'Hello', 'datetime' => '2014-05-05 12:08 PM'),
    array('ID' => 2, 'title' => 'Amazing Pic', 'datetime' => '2014-05-06 11:08 PM'),
    array('ID' => 3, 'title' => 'Style', 'datetime' => '2014-05-02 9:08 PM'),
    array('ID' => 4, 'title' => 'Hello World', 'datetime' => '2014-05-01 5:08 PM')
);

My question is how do I sort by title and datetime? I spent quite some time searching on this. But I just found sort by two columns but with the same data type. I am now struggling to do this because I believe mine involve strtotime() function as this involves time.

This is what I have at this moment

function querySort ($x, $y) {
    return strcasecmp($x['title'], $y['title']);
}

function cmp($a, $b){
    $ad = strtotime($a['datetime']);
    $bd = strtotime($b['datetime']);
    return ($ad-$bd);
}

usort($mylist , 'querySort');

usort($mylist , 'cmp');

Could anyone help me on how to achieve this?

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
UserProg
  • 629
  • 1
  • 8
  • 22

4 Answers4

0

It looks like you need array_multisort. You can visit following link for more information http://www.php.net//manual/en/function.array-multisort.php

The basic idea would be cleared from example#2 on above link.

Rohit Jangid
  • 1,077
  • 1
  • 8
  • 19
0

The second usort overrides the output of the first usort.

What you need to do is merge the 2 sorting functions to order themselves by title and then date time.

Pseudocode would be :

function SortByTitleAndDateTime($a,$b)
    1) SortByTitle and return the value if it is non zero.
    2) If SortByTitle is zero (meaning the 2 values are the same in terms of title), 
        do SortByDateTime

In the end, you'd only need to call usort(array, SortByTitleAndDateTime).

Zaenille
  • 1,384
  • 9
  • 16
0

Try this,

$mylist = array(
    array('ID' => 1, 'title' => 'Hello', 'datetime' => '2014-05-05 12:08 PM'),
    array('ID' => 2, 'title' => 'Amazing Pic', 'datetime' => '2014-05-06 11:08 PM'),
    array('ID' => 3, 'title' => 'Style', 'datetime' => '2014-05-02 9:08 PM'),
    array('ID' => 4, 'title' => 'Hello World', 'datetime' => '2014-05-01 5:08 PM'),
     array('ID' => 5, 'title' => 'Boy', 'datetime' => '2014-05-01 5:08 PM')
);


// Obtain a list of columns
foreach ($mylist as $key => $row) {
    $dTime[$key]  = $row['datetime'];
    $title[$key] = $row['title'];
}

// Sort the data with dTimedescending, titleascending
// Add $mylist as the last parameter, to sort by the common key
array_multisort($dTime, SORT_DESC, $title, SORT_ASC, $mylist);

Your output will be

Array
(
    [0] => Array
        (
            [ID] => 2
            [title] => Amazing Pic
            [datetime] => 2014-05-06 11:08 PM
        )

    [1] => Array
        (
            [ID] => 1
            [title] => Hello
            [datetime] => 2014-05-05 12:08 PM
        )

    [2] => Array
        (
            [ID] => 3
            [title] => Style
            [datetime] => 2014-05-02 9:08 PM
        )

    [3] => Array
        (
            [ID] => 5
            [title] => Boy
            [datetime] => 2014-05-01 5:08 PM
        )

    [4] => Array
        (
            [ID] => 4
            [title] => Hello World
            [datetime] => 2014-05-01 5:08 PM
        )

)

I have added ID no 5 for demonstration, so that date wise sorted in descending order and title wise sorted in ascending order

shatheesh
  • 633
  • 6
  • 10
0

I ended up with something like Mark Gabriel's logic. This will sort out the title first, and followed by the date time. So here it is:

function SortByTitleAndDateTime($a,$b){
    if ( strcasecmp($a['title'], $b['title']) != 0 ){
        return strcasecmp($a['title'], $b['title']);
    }
    else{
        $ad = strtotime($a['datetime']);
        $bd = strtotime($b['datetime']);
        return ($ad-$bd);
    }
}
usort($mylist, 'SortByTitleAndDateTime');

Hope this helps other people as well.

UserProg
  • 629
  • 1
  • 8
  • 22