Question

So I have an array called $links

array(
    [0] = array(
        'type' => 'thread'
        'url' => 'blah blah blah'
    ),
    [1] = array(
        'type' => 'media'
        'url' => 'blah blah blah'
    ),
    [2] = array(
        'type' => 'website'
        'url' => 'blah blah blah'
    )
);

What I am trying to do is sort this array alphabetically using "type". For this I am using usort()

usort($links, create_function('$b, $a', 'return $a["type"] - $b["type"];'));

The problem is, this is not actually sorting the array... all it does is REVERSE the array. After running through, I get Website > Media > Thread. If I process it a second time, it reverses back to Thread > Media > Website.

The final result should be Media > Thread > Website. Am I missing something? Why is this not sorting correctly?

Was it helpful?

Solution

Try this, instead:

usort($links, create_function('$a, $b', 'return strcmp($a["type"], $b["type"]);'));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top