![]() |
Array Sorting
I know how to sort an array by comparing each of the values in the array and then sorting them according to less than, greater than or equal to each other. However, given the case where I had an array of arrays (an n-dimensional array). How would I sort? Take this example:
Quote:
|
Re: Array Sorting
So if I understand correctly, the primary sort is on the last name? Then if there's a tie between two keys, the tie is broken by the first name?
There's no "special" algorithm for this. It's just a normal sort. In fact, something like this is so common that PHP supports a function that let's you, the programmer, specify the comparison function. usort(array, callback) You pass through your own callback function that will determine whether two values are less than, equal, or greater than eachother. In the event that your last name is equal, you can return the value corresponding to a second comparison of the first names. Warning: Untested. :
Is that the sort of thing you were looking for? |
Re: Array Sorting
That was what I suspected but I wasn't quite sure how to implement. I actually find a great tutorial http://www.the-art-of-web.com/php/sortarray/ which is exactly like what you're stating, I came up with the following:
:
|
Re: Array Sorting
I understand everything you're saying up until this point:
Quote:
Repeated keys? Maybe it would be helpful to explain the problem objectively, and why the solution you posted is insufficient? |
Re: Array Sorting
I wrote a class which you might be able to use and a sample:
:
|
Re: Array Sorting
An array of the following structure Sane:
:
|
Re: Array Sorting
how do you compare apples to oranges?
|
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: :
(Sorting on the 1st, 2nd, 3rd or 4th key. Doesn't matter. They are all the same. :
(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? |
Re: Array Sorting
Yeah... I know... recursion... T_T The bane of my existence. I've written about one recursive function in my life that I've understood lol. Not sure what you mean by your last sentence though. In any case I modified the sorting function to the following:
:
|
Re: Array Sorting
Zoh! What the hank are you doing all that for!? :| Seems like horrible coding practice to me.
What I meant by my last sentence, is if you wanted everything sorted as though the depth of the arrays didn't matter, why not flatten it into one level, then sort it? I don't see at all why you'd possibly want to sort an array like this, and which of the many ways you could have it sorted, you want. You could have everything sorted as though the depth did not matter. You could have each branch sorted separately. You could do the latter, and order the remaining multidimensional arrays by depth. You could do the latter, and order each array by how the first entry compares to the last entry in the last multidimensional array. You could have each level sorted (as opposed to branch). Then all the corresponding permutations that go along with that... So on and so forth... |
| All times are GMT -5. The time now is 3:33 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC