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.

I have an array like one below. Currently it is sorted alphabetically by the OwnerNickName field. Now i want to brig the array entry with OwnerNickName 'My House' as the first entry of the array and rest sorted alphabetically by OwnerNickName. Any idea?

Array
(
    [0318B69D-5DEB-11DF-9D7E-0026B9481364] => Array
        (
            [OwnerNickName] => andy
            [Rooms] => Array
                (
                    [0] => Array
                        (
                            [Label] => Living Room
                            [RoomKey] => FC795A73-695E-11DF-9D7E-0026B9481364
                        )

                )

        )

    [286C29DE-A9BE-102D-9C16-00163EEDFCFC] => Array
        (
            [OwnerNickName] => anton
            [Rooms] => Array
                (
                    [0] => Array
                        (
                            [Label] => KidsRoom
                            [RoomKey] => E79D7991-64DC-11DF-9D7E-0026B9481364
                        )

                    [1] => Array
                        (
                            [Label] => Basement
                            [RoomKey] => CC12C0C4-68AA-11DF-9D7E-0026B9481364
                        )

                    [2] => Array
                        (
                            [Label] => Family Room
                            [RoomKey] => 67A280D4-64D9-11DF-9D7E-0026B9481364
                        )

                )

        )

    [8BE18F84-AC22-102D-9C16-00163EEDFCFC] => Array
        (
            [OwnerNickName] => mike
            [Rooms] => Array
                (
                    [0] => Array
                        (
                            [Label] => Family Room
                            [RoomKey] => 1C6AFB39-6835-11DF-9D7E-0026B9481364
                        )

                )

        )

    [29B455DE-A9BC-102D-9C16-00163EEDFCFC] => Array
        (
            [OwnerNickName] => My House
            [Rooms] => Array
                (
                    [0] => Array
                        (
                            [Label] => Basement
                            [RoomKey] => 61ECFAB2-6376-11DF-9D7E-0026B9481364
                        )

                    [1] => Array
                        (
                            [Label] => Rec Room
                            [RoomKey] => 52B8B781-6376-11DF-9D7E-0026B9481364
                        )

                    [2] => Array
                        (
                            [Label] => Deck
                            [RoomKey] => FFEB4102-64DE-11DF-9D7E-0026B9481364
                        )

                    [3] => Array
                        (
                            [Label] => My Room2
                            [RoomKey] => 112473E4-64DF-11DF-9D7E-0026B9481364
                        )

                    [4] => Array
                        (
                            [Label] => Bar Room
                            [RoomKey] => F82C47A8-64DE-11DF-9D7E-0026B9481364
                        )

                )

        )

)
share|improve this question
    
Do you need to permanently rearrange the array? Or is the sort order variable? –  dnagirl Jun 2 '10 at 12:25
    
permanently reorder –  Mithun Jun 2 '10 at 12:35
add comment

2 Answers

up vote 3 down vote accepted

You may want to implement your own sorting function, e.g.:

function cmp($a, $b)
{
    if ($a['OwnerNickName'] == $b['OwnerNickName']) {
        return 0;
    }
    if ($a['OwnerNickName'] == 'My House') {
        return -1;
    } else if ($b['OwnerNickName'] == 'My House') {
        return 1;
    }
    return ($a['OwnerNickName'] < $b['OwnerNickName']) ? -1 : 1;
}    
usort($array, 'cmp');
share|improve this answer
4  
Maybe use uasort if you want to keep your keys –  Serty Oan Jun 2 '10 at 12:40
add comment

If you want to change your mind about which index to sort on or which value should be special, something like this might suit:

function specialSort(array &$array, $index, $specialvalue){
    $callback = function($a,$b) use ($index, $specialvalue) {  //closure
       if ($a[$index] == $b[$index]) return 0;
       if ($a[$index] == $specialvalue) return -1;
       if ($b[$index] == $specialvalue) return 1;
       return ($a[$index] < $b[$index]) ? -1 : 1;
    }  ;

    uasort($array, $callback);
}

$arr=array(
    'a'=>array('thing'=>'yay','who'=>'owee'),
    'foo'=>array('thing'=>'boo','who'=>'wik'),
    'd'=>array('thing'=>'kil','who'=>'ilo'),
    'b'=>array('thing'=>'ser','who'=>'uyt'),
    'zed'=>array('thing'=>'efv','who'=>'qet')
);

specialSort($arr,'who','ilo');
print_r($arr);

Gives the result:

Array
(
    [d] => Array
        (
            [thing] => kil
            [who] => ilo //special value
        )

    [a] => Array
        (
            [thing] => yay
            [who] => owee
        )

    [zed] => Array
        (
            [thing] => efv
            [who] => qet
        )

    [b] => Array
        (
            [thing] => ser
            [who] => uyt
        )

    [foo] => Array
        (
            [thing] => boo
            [who] => wik
        )

)
share|improve this answer
add comment

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.