Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8874589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:45:35+00:00 2026-06-14T18:45:35+00:00

I have problem with sorting this array: Array ( [0] => stdClass Object (

  • 0

I have problem with sorting this array:

Array
(
    [0] => stdClass Object
        (
            [id] => 120
            [registration_date] => 2012-10-19 16:57:46
            [username] => Jeff
        )
    [1] => stdClass Object
        (
            [id] => 121
            [registration_date] => 2012-12-23 16:57:46
            [username] => Peter
        )
    [2] => stdClass Object
        (
            [id] => 122
            [registration_date] => 2012-11-30 16:57:46
            [username] => Susan
        )

)

It is stored in variable:

$unsorted_users

I want to order this array DESC by registration_date so it looks like this:

Array
    (
        [0] => stdClass Object
            (
                [id] => 121
                [registration_date] => 2012-12-23 16:57:46
                [username] => Peter

            )
        [1] => stdClass Object
            (
                [id] => 122
                [registration_date] => 2012-11-30 16:57:46
                [username] => Susan

            )
        [2] => stdClass Object
            (
                [id] => 120
                [registration_date] => 2012-10-19 16:57:46
                [username] => Jeff
            )

    )

and it is ordered by registration_date like:

2012-12-23 16:57:46
2012-11-30 16:57:46
2012-10-19 16:57:46

Instead of original where it was like:

2012-10-19 16:57:46
2012-12-23 16:57:46
2012-11-30 16:57:46

I am using this code, but it is not working good (the print_r($sorted_users); output of $sorted_users is “1”. I don’t why it is 1 and not sorted array.)

$sorted_users = usort($unsorted_users, function($a, $b) {
   return $a['registration_date'] - $b['registration_date'];
});

Any advice why is my code for $sorted_users wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:45:37+00:00Added an answer on June 14, 2026 at 6:45 pm

    You can’t substract (that) strings. If you want to compare two strings, use the strcmp() function like this:

    <?php
    
    //  setup
    $unsorted_users = array();
    
    $t = new stdClass(); 
    $t->id = 120; $t->username = 'jeff'; $t->registration_date = '2012-10-19 16:57:46'; 
    $unsorted_users[] = $t;
    
    $t = new stdClass(); 
    $t->id = 121; $t->username = 'Peter'; $t->registration_date = '2012-12-23 16:57:46'; 
    $unsorted_users[] = $t;
    
    $t = new stdClass(); 
    $t->id = 122; $t->username = 'Susan'; $t->registration_date = '2012-11-30 16:57:46'; 
    $unsorted_users[] = $t;
    // end setup
    
    
    $sorted_users =    // this var is nor relevant as it shoud be allways true
    usort($unsorted_users, function($a, $b) {
       return strcmp ($a->registration_date, $b->registration_date) ;
    });
    
    echo '<pre>';
    print_r($unsorted_users)   // that is in fact ordered!
    
    ?>
    

    fiddle here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very strange array sorting related problem in PHP that is driving
I have a complex table pulled from a multi-ActiveRecord object array. This listing is
I got problem for sorting array of nsdate. so i have array of date
Im stuck on a sorting problem, I have an array with 10 numbers (1-10)
I have a problem with sorting of an array. $infoGroup is the result of
I have problem in sorting an array in ascending form and i don't know
I have a problem with showing results of method which sorting Person object by
I have a problem with sorting... I need to sort NSArray containing NSDictionaries .
I have a problem with sorting NSTableColumn contents. In my NSTableView there are three
I have a problem with sorting items in table in PHP. Here is what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.