![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
King of Portal
|
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:
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
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. PHP Syntax (Toggle Plain Text)
Is that the sort of thing you were looking for? |
|
|
|
|
|
#3 |
|
King of Portal
|
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:
PHP Syntax (Toggle Plain Text)
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#4 | |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
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? |
|
|
|
|
|
|
#5 |
|
Programmer
Join Date: Nov 2007
Posts: 86
Rep Power: 1
![]() |
Re: Array Sorting
I wrote a class which you might be able to use and a sample:
php Syntax (Toggle Plain Text)
Last edited by mbd; Jan 23rd, 2008 at 1:26 PM. Reason: made the example better |
|
|
|
|
|
#6 |
|
King of Portal
|
Re: Array Sorting
An array of the following structure Sane:
PHP Syntax (Toggle Plain Text)
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Nov 2007
Posts: 86
Rep Power: 1
![]() |
Re: Array Sorting
how do you compare apples to oranges?
|
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
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? |
|
|
|
|
|
#9 |
|
King of Portal
|
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:
php Syntax (Toggle Plain Text)
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis |
|
|
|
|
|
#10 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
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... Last edited by Sane; Jan 24th, 2008 at 10:08 AM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| tasm sorting 5 integers with array | akioshin | Assembly | 15 | Oct 30th, 2007 1:45 AM |
| sorting an array by field | cwl157 | C | 4 | Apr 4th, 2007 2:48 PM |
| Sorting an array of objects | oNe8 | Java | 2 | Feb 22nd, 2006 10:59 PM |
| Sorting a Numeric Array | little_valaree | Java | 2 | Nov 21st, 2005 11:00 AM |
| Installing IPB 2.03 | bh4575 | Other Web Development Languages | 0 | Apr 23rd, 2005 2:36 AM |