473,236 Members | 1,735 Online

# multi sorting multi dimensional array?

Hey all,

I have been using usort to sort my multi dimensional arrays ...

function cmp(\$a, \$b){
if(\$a[0] == \$b[0]){
return 0;
}
return (\$a[0] < \$b[0]) ? -1 : 1;
}
usort(\$searchResults, 'cmp');

Which works fine, but I need to sort by one element in the array, then
another, preserving the previous sort.

lets say the array is as follows:

array[0] = array[0] = foo
[1] = 2
[1] = array[0] = foo
[1] = 0
[2] = array[0] = bar
[1] = 1
[3] = array[0] = bar
[1] = 0
[4] = array[0] = foo
[1] = 1
[5] = array[0] = bar
[1] = 2

Initially, I sort the array in to groups based on array[?][0] using
usort as above so the output is :

foo, 2
foo, 0
foo, 1
bar, 1
bar, 2
bar, 0

Next though, I would like to sort this output by number (array[?][1])
in the groups so that the output is :

foo, 0
foo, 1
foo, 2
bar, 0
bar, 1
bar, 2

Is this possible? I am completely at a loss as to how to do this ...

Cheers,

Eclectic
Jul 17 '05 #1
5 1772
Eclectic <ec*********@hotmail.com> wrote:
foo, 2 | foo, 0 | foo, 1 | bar, 1 | bar, 2 | bar, 0

Next though, I would like to sort this output by number (array[?][1])
in the groups so that the output is :

foo, 0 | foo, 1 | foo, 2 | bar, 0 | bar, 1 | bar, 2

Is this possible? I am completely at a loss as to how to do this ...

Yes, and it's quite simple, int the cmp you should first check on index
0 is (eg -strcmp), if the result equals 0 that means this and next
element have the same index 0 (eg foo), thus instead of sorting on 0 for
these 2 elements you should use index 1.

The actual cmp function is left to the execise of the reader :)

Jul 17 '05 #2
I understand that, but what I want do is sort the aray by index 0 so
that all the 'foo' and 'bar' are grouped, then keeping these grouped,
sort by index 1 (number) so that the results are grouped by index 0,
and each group is in order of number (index 1) ... make sence?
Jul 17 '05 #3
On 16 Mar 2005 23:06:48 -0800, Eclectic wrote:
what I want do is sort the aray by index 0 so
that all the 'foo' and 'bar' are grouped, then keeping these grouped,
sort by index 1 (number) so that the results are grouped by index 0,
and each group is in order of number (index 1)

That's exactly what he said. See http://php.net/usort
In the compare function "cmp(\$a,\$b)", first check the order of \$a[0] and
\$b[0], if not equal return -1 or 1 from function, if equal return the
order of \$a[1] and \$b[1].
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #4
Eclectic <ec*********@hotmail.com> wrote:
I understand that, but what I want do is sort the aray by index 0 so
that all the 'foo' and 'bar' are grouped, then keeping these grouped,
sort by index 1 (number) so that the results are grouped by index 0,
and each group is in order of number (index 1) ... make sence?

That is what I (tried) to explain:

function comparator
{
compare index 0

if equal
compare index 1

return result
}

The actual code is trivial and left for your excersize.

Jul 17 '05 #5
*click* ... cheers ... got it :)
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 3 by: Paul Kirby | last post by: Hello All I am trying to update me code to use arrays to store a group of information and I have come up with a problem sorting the multiple array :( Array trying to sort: (7 arrays put into... 2 by: ip4ram | last post by: I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =... 18 by: bsder | last post by: Hi, Can anyone please tell me how to calculate the size of the following 4-dimensional array, and now to use qsort for sorting on this array? double sp = { 4.0, 5.0, 6.0 }; double spa = { {... 4 by: Robert P. | last post by: I can easily store a one-dimensional array in viewstate ( see Test1 ) If I try storing a multi-dimensional array in the viewstate it's crapping out on me when it goes to serialize the array (not... 1 by: Christopher Miles | last post by: Hello, I am trying to sort this array by the time field , and have all the other fields move accordingly to match .. Group_Array(0) - name Group_Array(1) - severity Group_Array(2) - Time... 4 by: Balaskas Evaggelos | last post by: Hi, does anyone know how i can sort a multi-dimensional array by a specific field ? for example i want to sort arr where n=2, but i need the data of every array to follow that order. ... 152 by: vippstar | last post by: The subject might be misleading. Regardless, is this code valid: #include void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {... 5 by: JC | last post by: Hi all, I have scoured the internet for articles on sorting multi-dimensional arrays of generic types e.g. T using the IComparer