1

I have an unexpected outcome of usort.

$ccpmail[] =  
    array(
            "mail_id"=>$evemailheader["mail_id"],
            "is_read"=>$evemailheader['is_read'],                                           
            "fromid"=>$evemailheader['from'],
            "fromname"=>$character_names[get_character_name($evemailheader['from'], $character_names)]['character_name'],
            "subject"=>$evemailheader['subject'],
            "labels"=>array("EVE Online"),
            'datestamp'=>strtotime($evemailheader['timestamp']),
            'date'=>$date
        );

$email[] =  
    array(
        "mail_id"=>$mailid,
        "is_read"=>$is_read,                                            
        "fromid"=>$message_parsed->getHeaderValue('from'),
        "fromname"=>$message_parsed->getHeader('from')->getPersonName(),
        "subject"=>$message_parsed->getHeaderValue('subject'),
        "labels"=>array("E-mail"),
        'datestamp'=>strtotime($message_parsed->getHeaderValue('date')),
        'date'=>$date->format('Y-m-d H:i:s')
    );


$allmail = array_merge($ccpmail, $email);

function sortByOrder($a, $b) {
return  $a['datestamp'] < $b['datestamp'];
}

usort($allmail, 'sortByOrder');
var_dump($allmail);

RESULTS IN:

array(133) {
[0]=>
array(8) {
    ["mail_id"]=>
    string(32) "bcfc5f6b0365738e58c99e0501b37210"
    ["is_read"]=>
    bool(true)
    ["fromid"]=>
    string(22) "EMAIL"
    ["fromname"]=>
    string(12) "NAME"
    ["subject"]=>
    string(4) "tset"
    ["labels"]=>
    array(1) {
        [0]=>
        string(6) "E-mail"
    }
    ["datestamp"]=>
    int(1485509147)
    ["date"]=>
    string(19) "2017-01-27 09:25:47"
}
[1]=>
array(8) {
    ["mail_id"]=>
    string(32) "317c556f35213548598f0bb838a237c1"
    ["is_read"]=>
    bool(true)
    ["fromid"]=>
    string(22) "EMAIL"
    ["fromname"]=>
    string(12) "NAME"
    ["subject"]=>
    string(5) "etest"
    ["labels"]=>
    array(1) {
        [0]=>
        string(6) "E-mail"
    }
    ["datestamp"]=>
    int(1485509066)
    ["date"]=>
    string(19) "2017-01-27 09:24:26"
}
[2]=>
array(8) {
    ["mail_id"]=>
    int(364172046)
    ["is_read"]=>
    bool(false)
    ["fromid"]=>
    int(90766569)
    ["fromname"]=>
    string(13) "NAME"
    ["subject"]=>
    string(2) "hi"
    ["labels"]=>
    array(1) {
        [0]=>
        string(10) "EVE Online"
    }
    ["datestamp"]=>
    int(1485507000)
    ["date"]=>
    object(DateTime)#3 (3) {
        ["date"]=>
        string(26) "2017-01-27 08:50:00.000000"
        ["timezone_type"]=>
        int(2)
        ["timezone"]=>
        string(1) "Z"
    }
}
[3]=>
array(8) {
    ["mail_id"]=>
    int(364160956)
    ["is_read"]=>
    bool(true)
    ["fromid"]=>
    int(793110520)
    ["fromname"]=>
    string(10) "NAME"
    ["subject"]=>
    string(30) "theres no brakes on this train"
    ["labels"]=>
    array(1) {
        [0]=>
        string(10) "EVE Online"
    }
    ["datestamp"]=>
    int(1485455100)
    ["date"]=>
    object(DateTime)#7 (3) {
        ["date"]=>
        string(26) "2017-01-26 18:25:00.000000"
        ["timezone_type"]=>
        int(2)
        ["timezone"]=>
        string(1) "Z"
    }
}
[4]=>
array(8) {
    ["mail_id"]=>
    int(364160959)
    ["is_read"]=>
    bool(true)
    ["fromid"]=>
    int(793110520)
    ["fromname"]=>
    string(10) "NAME"
    ["subject"]=>
    string(10) "safe word?"
    ["labels"]=>
    array(1) {
        [0]=>
        string(10) "EVE Online"
    }
    ["datestamp"]=>
    int(1485455100)
    ["date"]=>
    object(DateTime)#5 (3) {
        ["date"]=>
        string(26) "2017-01-26 18:25:00.000000"
        ["timezone_type"]=>
        int(2)
        ["timezone"]=>
        string(1) "Z"
    }
}
[5]=>
array(8) {
    ["mail_id"]=>
    int(364160957)
    ["is_read"]=>
    bool(true)
    ["fromid"]=>
    int(1228369447)
    ["fromname"]=>
    string(7) "NAME"
    ["subject"]=>
    string(4) "spam"
    ["labels"]=>
    array(1) {
        [0]=>
        string(10) "EVE Online"
    }
    ["datestamp"]=>
    int(1485455100)
    ["date"]=>
    object(DateTime)#6 (3) {
        ["date"]=>
        string(26) "2017-01-26 18:25:00.000000"
        ["timezone_type"]=>
        int(2)
        ["timezone"]=>
        string(1) "Z"
    }
}
[6]=>
array(8) {
    ["mail_id"]=>
    int(364160934)
    ["is_read"]=>
    bool(true)
    ["fromid"]=>
    int(94312752)
    ["fromname"]=>
    string(19) "NAME"
    ["subject"]=>
    string(4) "spam"
    ["labels"]=>
    array(1) {
        [0]=>
        string(10) "EVE Online"
    }
    ["datestamp"]=>
    int(1485455040)
    ["date"]=>
    object(DateTime)#11 (3) {
        ["date"]=>
        string(26) "2017-01-26 18:24:00.000000"
        ["timezone_type"]=>
        int(2)
        ["timezone"]=>
        string(1) "Z"
    }
}

As you can see the array is not sorted like how I want it. I want the lowest timestamp at top. Can anyone help me out?

2
  • 1
    return $a['datestamp'] > $b['datestamp'];? Commented Jan 27, 2017 at 21:40
  • Did not work... the array sorted just like array_reverse() Commented Jan 27, 2017 at 22:00

2 Answers 2

1

If you use PHP7 or later, you could use the spaceship operator, like so:

function sortByOrder($a, $b) {
    return  $a['datestamp'] <=> $b['datestamp'];
}

This will sort it like you want. If not using PHP7 or higher, you would have to make some if statements to see if it's higher, lower or equal.

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

Comments

0

Your comparison function needs to return an integer greater than, less than or equal to zero in order to sort properly. (See the definition of value_compare_func here.) This expression:

return  $a['datestamp'] < $b['datestamp'];

will only return true or false (1 or 0,) neither of which is the correct value to sort earlier timestamps before later ones (-1).

You can add one more comparison so that it will the values necessary to sort correctly.

function sortByOrder($a, $b) {
    if ($a['datestamp'] < $b['datestamp']) return -1;    // return -1 if $a is earlier
    return  $a['datestamp'] > $b['datestamp'];    // return 1 if $a is later, or 0 if equal
}

As noted in the other answer, if you're using PHP 7 there is an operator (<=>) that will let you do this with one statement.

2 Comments

This has exactly the same outcome unfortunatly
Great! Glad to hear it.

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.