tags:

views:

370

answers:

5

How can I sort this array by the value of the "order" key? Even though the values are currently sequential, they will not always be.

Array
(
    [0] => Array
        (
            [hashtag] => a7e87329b5eab8578f4f1098a152d6f4
            [title] => Flower
            [order] => 3
        )

    [1] => Array
        (
            [hashtag] => b24ce0cd392a5b0b8dedc66c25213594
            [title] => Free
            [order] => 2
        )

    [2] => Array
        (
            [hashtag] => e7d31fc0602fb2ede144d18cdffd816b
            [title] => Ready
            [order] => 1
        )
)
+2  A: 
function aasort (&$array, $key) {
    $sorter=array();
    $ret=array();
    reset($array);
    foreach ($array as $ii => $va) {
        $sorter[$ii]=$va[$key];
    }
    asort($sorter);
    foreach ($sorter as $ii => $va) {
        $ret[$ii]=$array[$ii];
    }
    $array=$ret;
}

aasort($your_array,"order");
Lo'oris
Take a look at http://en.wikipedia.org/wiki/Sorting_algorithm for sorting algoritms to be able to sort your data as efficient as possible
Robert
This could be inefficient, not *(only)* because of the algorithm, but because php is dumb slow on iterations. (and `foreach` makes - and iterates over - copies of arrays in memory silently) As someone (*studer* and *fabry*) already said below, PHP has a number of sort contraptions (`usort`,`uasort`,`uksort`) which take custom `cmp` callbacks as arguments.
ZJR
+3  A: 

I use this function :

function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
    $sort_col = array();
    foreach ($arr as $key=> $row) {
        $sort_col[$key] = $row[$col];
    }

    array_multisort($sort_col, $dir, $arr);
}


array_sort_by_column($array, 'order');
Tom Haigh
+7  A: 

I usually use usort, and pass my own comparison function. In this case, it is very simple:

function compareOrder($a, $b)
{
  return $a['order'] - $b['order'];
}

usort($array, 'compareOrder');
Jan Fabry
Dammit, I was 30 seconds slower. Isn't it $a - $b though?
christian studer
I always get this wrong. Let me think from the manual: the return value must be less than zero if the first argument is considered less than the second. So if `$a['order']` is 3, and `$b['order']` is 6, I will return -3. Correct?
Jan Fabry
Well, you return b - a, so it will be 3. And thus sorted incorrectly.
christian studer
Ah. OK. I was using non-logical arithmetic, where the idea in your head does not match the words you produce. This is studied most frequently on Friday afternoons.
Jan Fabry
+8  A: 

Try a usort: If you don't have access to anonymous functions yet (Before PHP 5.3), you'll have to define a sorting function first:

function sortByOrder($a, $b) {
    return $a['order'] - $b['order'];
}

usort($myArray, 'sortByOrder');
christian studer
Do you mean you wouldn't use `usort` in 5.3, because you have anonymous functions? Wouldn't you just pass the anonymous function as the second parameter to `usort`?Of course, you can also create a pseudo-anonymous function pre 5.3 using `create_function`, but this is not recommended.
Jan Fabry
My phrasing is a little bit off, I'll edit it. What I meant is that if you're not on PHP 5.3, you need to create a special function just for this particular sorting (Which is somehow not very elegant). Otherwise you could use an anonymous function right there.
christian studer
A: 

Can anyone help me with this one? I'm trying to sort this multidimensional array by "age" where the keys are strings and not an index number?

Array
(
    [george1] => Array
        (
            [name] => Georgia
            [age] => 32
        )

    [george2] => Array
        (
            [name] => George
            [age] => 16
        )

    [george3] => Array
        (
            [name] => Georgette
            [age] => 54
        )

    [george4] => Array
        (
            [name] => Georgina
            [age] => 27
        )

)

I just can't seem to get the sort right.... The array

Sam Critchley
meta-hint: You are supposed to ask a new question, not to answer to an existing one.
Lo'oris