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.

hello I want to sort my object array by date, which is like that:

$array = array( 
[0] = array ('date' => '2012-05-15', 'name' => 'jon')
[1] = array ('date' => '2012-05-10', 'name' => 'jon')
[2] = array ('date' => '2012-05-09', 'name' => 'jon')
[3] = array ('date' => '2012-05-16', 'name' => 'jon')
);

can I do that somehow? and what method I have to use? Thanks.

share|improve this question
    
Where do you get the data from? If you pull the data out of a database, you could edit the SQL query. –  Ahatius Apr 25 '12 at 5:18
    
possible duplicate of How to sort a multidimensional array by a certain key? –  deceze Apr 25 '12 at 5:19
    
you cold edit the sql query, order by date :) so the result data will be order by date, it depends on you if you want ascending or descending 1keydata.com/sql/sqlorderby.html –  SuperNoob Apr 25 '12 at 5:25
    
yes I get this from database, but for my reasons I cant do sorting in database :) –  Donatas Veikutis Apr 25 '12 at 5:25

4 Answers 4

up vote 1 down vote accepted

uasort() is another option.

$array = array( 
array ('date' => '2012-05-15', 'name' => 'jon'),
array ('date' => '2012-05-10', 'name' => 'jon'),
array ('date' => '2012-05-09', 'name' => 'jon'),
array ('date' => '2012-05-16', 'name' => 'jon')
);

function date_sort($a, $b)
{
    if ( $a['date'] < $b['date'] ) return -1;
    if ( $a['date'] > $b['date'] ) return 1;
    return 0;
}

uasort($array, 'date_sort');
var_dump($array);
share|improve this answer

You could use usort() with some custom logic to accomplish this:

$array = array(
    array('date' => '2012-05-15', 'name' => 'jon'),
    array('date' => '2012-05-10', 'name' => 'jon'),
    array('date' => '2012-05-09', 'name' => 'jon'),
    array('date' => '2012-05-16', 'name' => 'jon')
);

function mySorter( $a, $b ) {
  if ( $a['date'] < $b['date'] ) return -1;
  if ( $a['date'] > $b['date'] ) return 1;
  return 0;
}

usort( $array, mySorter );

When all is said and done, $array now looks like this:

Array
(
    [0] => Array
        (
            [date] => 2012-05-09
            [name] => jon
        )
    [1] => Array
        (
            [date] => 2012-05-10
            [name] => jon
        )
    [2] => Array
        (
            [date] => 2012-05-15
            [name] => jon
        )
    [3] => Array
        (
            [date] => 2012-05-16
            [name] => jon
        )
)
share|improve this answer

Obviously, usort is the answer. But, if you want to specify the sorting criterion (sort by date or sort by name), you can do the following:

<?php
$sortOrder = 'date'; // default is to sort by date
$array = array( 
[0] = array ('date' => '2012-05-15', 'name' => 'jon')
[1] = array ('date' => '2012-05-10', 'name' => 'jon')
[2] = array ('date' => '2012-05-09', 'name' => 'jon')
[3] = array ('date' => '2012-05-16', 'name' => 'jon')
);

function date_sort($a, $b)
{
    global $sortOrder;

    if( $sortOrder == 'date' ) {
        if ( strtotime( $a['date'] ) < strtotime( $b['date'] ) ) return -1;
        if ( strtotime( $a['date'] ) > strtotime( $b['date'] ) ) return 1;
        return 0;
    } else if( $sortOrder == 'name' ) {
        return strcmp( $a['name'], $b['name'] );
    }
}

// sorted by date
uasort($array, 'mysort');


// sorted by name
$sortOrder = 'name';
uasort($array, 'mysort');
?>

Hope this helps.

share|improve this answer
$array = array(
array ('date' => '2012-05-15', 'name' => 'jon'), 
array ('date' => '2012-05-10', 'name' => 'jon'),
array ('date' => '2012-05-09', 'name' => 'jon'),
array ('date' => '2012-05-16', 'name' => 'jon')

);

sort($array);

echo '<pre>';
print_r($array);
echo '</pre>';

Use already existing methods.

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.