2

I have an array that contains multiple objects with many properties.

I want to sort it in PHP base on two object properties

Here's a sample array of objects to give you an idea of what data i'm dealing with:

Array (
    [0] => stdClass Object (
        [username] => user98
        [sender_id] => 98
        [date_sent] => 2012-07-25 00:52:11
        [not_read] => 0
    )
    [1] => stdClass Object (
        [username] => user87
        [sender_id] => 87
        [date_sent] => 2012-07-25 00:59:15
        [not_read] => 1
    )
    [2] => stdClass Object (
        [username] => user93
        [sender_id] => 93
        [date_sent] => 2012-07-25 00:52:13
        [not_read] => 2
    )
    [3] => stdClass Object (
        [username] => user5
        [sender_id] => 5
        [date_sent] => 2012-07-25 00:52:16
        [not_read] => 0
    )
)

I need to sort it resulting to this array:

Array (
    [1] => stdClass Object (
        [username] => user87
        [sender_id] => 87
        [date_sent] => 2012-07-25 00:59:15
        [not_read] => 1
    )
    [2] => stdClass Object (
        [username] => user93
        [sender_id] => 93
        [date_sent] => 2012-07-25 00:52:13
        [not_read] => 2
    )
    [3] => stdClass Object (
        [username] => user5
        [sender_id] => 5
        [date_sent] => 2012-07-25 00:52:16
        [not_read] => 0
    )

    [0] => stdClass Object (
        [username] => user98
        [sender_id] => 98
        [date_sent] => 2012-07-25 00:52:11
        [not_read] => 0
    )


)

The sorting is based on the date property and the not_read property of the object, the not_read > 0 is prioritized first in the sorting, then it will look at the date_sent property and sort it on the latest date_sent. Note that it is not based on who has the higher not_read property.

Then those with 0 not_read property will be sorted by latest date_sent.

Can anyone help me with this program?

thanks a lot of looking!

2 Answers 2

4

You need to use a user-defined sort function:

function sortByDate($a, $b)
{
    if($a->not_read > $b->not_read)
        return 1;
    if($a->not_read < $b->not_read)
        return -1;
    if(strtotime($a->date_sent) > strtotime($b->date_sent))
        return 1;
    if(strtotime($a->date_sent) < strtotime($b->date_sent))
        return -1;
    return 0;
}

Then call it with usort:

usort($array_to_sort, 'sortByDate');

The array passed in will now be sorted.

Sign up to request clarification or add additional context in comments.

2 Comments

i got confused from this code, i mean if the if statement in the $a->not_read returns already, then how will the code reach to the strtotime?
@KevinLee since your first sorting criteria is for the not_read property, the date_sent doesn't matter. Only for the objects where the not_read property are the same would you need to worry about the date_sent property.
1
function sortByDate($a, $b)
{
    if($a->not_read > 0 && $b->not_read == 0)
        return -1;
    if($b->not_read > 0 && $a->not_read == 0)
        return 1;
    if ($a->not_read == 0 && $b->not_read == 0 || $a->not_read > 0 && $b->not_read > 0){
        if(strtotime($a->date_sent) > strtotime($b->date_sent))
            return -1;
        if(strtotime($a->date_sent) < strtotime($b->date_sent))
            return 1;
    }

    return 0;
}

usort($array_to_sort, 'sortByDate');

Note: I would of made an edit to Patrick's but I wasn't sure if mine even worked. He was on the right track.

3 Comments

@MatthewScragg the conditional isn't correct for the not_read property in your sort function. You checking only if the two comparing objects have a not_read property of > 0 and the other MUST have a not_read property of 0, in the example the OP gives there are three values given for not_read: 0,1,2. The sort function will not sort those objects properly.
@Patrick Read the OP again, it appears he doesn't care what the not_read is, just as long not_read > 0 is in front AND still sorted by date.
@MatthewScragg ah, missed that part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.