## Sorting multidimensional array by two criteria

### Sorting multidimensional array by two criteria

I am trying to sort a multidimensional array by two criteria e.g.

\$output = array(1234, 3),
array(5678, 3),
array(1234, 2)

to produce

1234, 2
1234, 3
5678, 3

I see that the following code from the manual allows me to sort by one
criteria but how do I use two?

function cmp(\$a, \$b)
{
return strcmp(\$a[1], \$b[1]);
}

usort(\$output, "cmp");

Thanks,

Chris

### Sorting multidimensional array by two criteria

> r>ay>1234, 2)
>
> 1234, >
> 1234, >
>
> I see that the following code from the manual allows me to>sort by one
> criteria but how d> I>use two?
>
> function>cmp(>a, \$b)
> {
> return strcmp(>a[>], \$>[1>);
>
> }
>
> usort(\$o>tp>t, "cmp");> >> > Thanks,
>
> Chris

Okay, a little light when on and I made life easier for myself
regarding previous post.

I was exploding a string such as '19 Nov 2009, 18:00', adding as part
of a multidimensional array, and then wanting to sort by day, hour.

The simple solution was to use strtotime and substr and concatenate
\$str[0] 1258588800 and \$str[1] 18 to get 125858880018 and sorting by
date, hour is easy.

Chris

### Sorting multidimensional array by two criteria

.oO(Chris)

JFTR: Just in case you need multi-column sorting somewhere else, have a
look at array_multisort(). It's a really powerful function, even though
its usage can be a bit complicated.

Micha

### Sorting multidimensional array by two criteria

> r>ay>1234, 2)
>
> 1234, >
> 1234, >
>
> I see that the following code from the manual allows me to>sort by one
> criteria but how d> I>use two?
>
> function>cmp(>a, \$b)
> {
> return strcmp(>a[>], \$>[1>);
>
> }
>
> usort(\$o>tp>t, "cmp");> >> > Thanks,
>
> Chris

function cmp(\$a, \$b, \$offset=0)
{
if (\$a[\$offset]==\$b[\$offset]) {
if (array_key_exists(\$offset+1, \$a) || array_key_exists(\$offset
+1, \$b)) {
return cmp(\$a, \$b, \$offset+1);
} else return 0;
} else ret<rn \$a[\$offset] < \$b[\$offset] ? -1 : 1;
}

.....or....
function cmp(\$a,\$b)
{
\$al=implode(' ',\$a);
\$bl=implode(' ',\$b);
return strcmp(\$al,\$bl);
}
....or....
(no, I got bored at this point - think up some more yourself)

C.

### Sorting multidimensional array by two criteria

On Nov 18, 1:29m, "C. ( http://www.yqcomputer.com/ )"

> > r>ay>1>34, 2)
>
> > t> >roduce
> >>>> 1234, 2 >>>> 1234, 3 >> > >678, 3
>
> > I see that the following code from the manual allows me to>s>rt by one
> > criteria but how d> I>u>e two?
>
> > function>c>p(\$a> >b)
> > {
> > return strcmp(>a[>]> \$b[>])> >>
> > }
>
> > usort(\$o>tp>t> "cmp");
> > Thanks,> >> > > Chris
>
> function cmp(\$a, >b, \$>ffset=0)
> {
> if (\$a[\$offset]=>\$b[\$offset]) {
> f (array_key_exists(\$offset+1, \$a) || array_k>y_exists(\$off>et
> +1, \$b)) {
> return cm>(\$a, \$b, \$offset+1);
> } e<se return \$a[\$offset]>< >b[\$o>fs>t] ? -1 : 1; >>
> }
>
> ....or...>
> >unction cmp(\$a,\$b)
> { >> \$al=implode(' ',\$a);> > \$bl=implode(' ',\$b);
>>>eturn strcmp>\$al,\$bl);}
>
> ...or....
> (no, I got bored at this point - t>in> up some more yourself)
>
> C.

Thanks C, that was the solution.

Chris