Thread: Array Sorting
View Single Post
Old Jan 23rd, 2008, 3:24 PM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,885
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Array Sorting

Oh! The number of dimensions is dynamically changing! ... Wow ...

Okay. So, now I would say it depends on the fine specifics of how you need it sorted. I can think of at least 3 ways right now given your structure. I am guessing you want each depth sorted with respect to itself, like so:

(
    ( 
        6, 
        6, 
        6, 
        6 
    ),

    ( 
        5, 
        5, 
        5, 
        5 
    ),

    (
        ( 
            6, 
            6, 
            6, 
            6 
        ),

        (
            4, 
            4, 
            4, 
            4 
        )
    )
)

Sorting on the 1st, 2nd, 3rd or 4th key. Doesn't matter. They are all the same.

(
    ( 
        5, 
        5, 
        5, 
        5 
    ),

    ( 
        6, 
        6, 
        6, 
        6
    ),

    (
        (
            4, 
            4, 
            4, 
            4
        ),

        ( 
            6, 
            6, 
            6, 
            6
        )
    )
)

So. Recursion. In your compare callback, while you're comparing each type, if you find that it's an array, call "usort" for that array from within the callback.

But you're going to have to let the callback function consistently know that an array of arrays should be considered greater than everything else (to get them all at the bottom).

Shouldn't have to tweak the usort that much.

But if you want everything sorted globally, which could potentially splice all your arrays into tiny bits... then why not just flatten it first?
Sane is offline   Reply With Quote