Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
What is the output right now? –  Mark Gabriel Jun 11 at 5:09
    
Another thing: It is not possible to sort by two parameter. If you sort by title and then by datetime, the result wont be different from a sort with only datetime. –  Y U NO WORK Jun 11 at 5:11
    
@MarkGabriel I have tried using array_multisort() but ended up with only title is sorted correctly but not the date time –  UserProg Jun 11 at 5:12
    
I found some solutions even in SO itself, it is possible to sort by more than one parameters. it is just that my parameter involves date time format, so this is little bit tricky for me. –  UserProg Jun 11 at 5:14
    
Well according to this code, your array is sorted according to the title and then, you sort it according to the datetime. So the first sorting is useless. The output you get is an array which is sorted according to the datetime. –  Xl-lord Jun 11 at 5:15

4 Answers 4

up vote 0 down vote accepted

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

share|improve this answer
    
let me give it a try first –  UserProg Jun 11 at 5:21
    
Awsome! it worked! thanks. I will post the answer. Thank you for your logic. +1 –  UserProg Jun 11 at 5:38
    
You're welcome. :) –  Mark Gabriel Jun 11 at 5:40

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.

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.