473,236 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,236 software developers and data experts.

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 ...

Please help! :)

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 <stdio.h> 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<Tinterface and have run into a real roadblack. There does not...
4
by: =?Utf-8?B?SGVucmlrIFNjaG1pZA==?= | last post by:
Hi, consider the attached code. Serializing the multi-dimensional array takes about 36s vs. 0.36s for the single-dimensional array. Initializing the multi-dimensional array takes about 4s...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.