1

I have an array that contain the values north, east, south or west.

For example I got an array with the values in this order: south, west and north.

Now I would like to sort the array like north, east, south and west.

So in my example the values should be in this order: north, south, west.

How can I do that?

Thanks!

Jordy
  • 4,719
  • 11
  • 47
  • 81
  • Please add some code what you did so far. Even if it's only the definition of the arrays. And maybe you can highlight the point where you got stuck with the problem better as well. – hakre Dec 28 '11 at 18:02
  • Often very helpful is the [comparison of array sorting functions](http://www.php.net/manual/en/array.sorting.php). – hakre Dec 28 '11 at 18:03
  • Can you give a little more detail, why do you want to sort it this way? What are you trying to do? As it is now, I do not see why you wouldn't just create an array with it sorted the way you want since you already know what it needs to contain. – JD Isaacks Dec 28 '11 at 18:03
  • you could try to give numeric values to each key with values north east south west etc.. and then sort by key with number are you using php? – Samuel Lopez Dec 28 '11 at 18:04
  • @John Issaacks, I think you understand me;-), but I need it sorted, because I make compare it with another (http://stackoverflow.com/questions/8105244/how-to-use-an-array-in-case/8105323), so if I don't sort it, it cannot be compared. – Jordy Dec 28 '11 at 18:05
  • possible duplicate of [Sort an array based on another array?](http://stackoverflow.com/questions/348410/sort-an-array-based-on-another-array) – hakre Dec 28 '11 at 18:09
  • If you are comparing it to a static array in a case statement like in the link you provided, then why not make all the arrays in the case statement sorted alphabetically and then just sort the array you are comparing to alphabetically? – JD Isaacks Dec 28 '11 at 18:12

2 Answers2

6

You could also use array_intersect(). It preserves the order of the first array. Give an array of all cardinal directions in the correct order as the first parameter and the array to sort as the second.

$cardinals = array( 'north', 'east', 'south', 'west' );
$input = array( 'south', 'west', 'north' );

print_r( array_intersect( $cardinals, $input ) );
JJJ
  • 32,902
  • 20
  • 89
  • 102
  • Interesting, I like three things (at least), it's concise *but* also very clear, doesn't use any callback and is as extensible (if one needs to add north-east etc.) as a callback solution. Neat! – zrvan Dec 28 '11 at 18:20
  • Yes, very neat. If you need to sort by keys, there is as well [`array_intersect_key`](http://php.net/array_intersect_key). – hakre Dec 28 '11 at 18:25
0

You could do something along the lines of this (I believe it's what Samuel Lopez is also suggesting in the comments):

$arr = array ('north', 'west', 'south', 'east', );

function compass_sort ($a, $b)
{
        $cmptable = array_flip (array (
                'north',
                /* you might want to add 'northeast' here*/
                'east',
                /* and 'southeast' here */
                'south',
                'west',
        ));

        $v1 = trim (mb_strtolower ($a));
        $v2 = trim (mb_strtolower ($b));

        if ( ! isset ($cmptable[$v1])
           || ! isset ($cmptable[$v2]))
        {
                /* error, no such direction */
        }

        return $cmptable[$v1] > $cmptable[$v2];
}

usort ($arr, 'compass_sort');

This assigns a number to each direction and sorts on that number, north will be assigned zero, east one (unless you add something in between) etc.

zrvan
  • 7,533
  • 1
  • 22
  • 23