471,825 Members | 1,061 Online

# sort 2 dimensional array alphabetically by second field in each row

Is there an easy way to sort a 2 dimensional array alphabetically by
the second field in each row?

Also, when I use sort() on a two dimensional array, it seems to work a
lot like array_reverse(). Can anyone tell me why?
Jul 17 '05 #1
9 17458
lawrence:
Is there an easy way to sort a 2 dimensional array alphabetically by
the second field in each row?
Well you could always use usort, which for most purposes should suffice.
However, be aware that this incurs a lot of overhead.

But also remember the words of Donald Knuth: "Premature optimization is the
root of all evil".
Also, when I use sort() on a two dimensional array, it seems to work a
lot like array_reverse(). Can anyone tell me why?

Works for me. Can you provide a repeatable example?

André Nęss
Jul 17 '05 #2
André Nęss wrote:
lawrence:

Is there an easy way to sort a 2 dimensional array alphabetically by
the second field in each row?

Well you could always use usort, which for most purposes should suffice.
However, be aware that this incurs a lot of overhead.

But also remember the words of Donald Knuth: "Premature optimization isthe
root of all evil".

Also, when I use sort() on a two dimensional array, it seems to work a
lot like array_reverse(). Can anyone tell me why?

Works for me. Can you provide a repeatable example?

André Nęss

as well, there is always the bubble sort method.

Jul 17 '05 #3
lawrence wrote:
Is there an easy way to sort a 2 dimensional array alphabetically by
the second field in each row?

Also, when I use sort() on a two dimensional array, it seems to work a
lot like array_reverse(). Can anyone tell me why?

You can always resort to using a bubble sort when sorting requirements
become, er, different.

Be aware that bubble sort is generally considdered an expensive way to
do things.

Jul 17 '05 #4
Terence wrote:
André Nęss wrote:
lawrence:

Is there an easy way to sort a 2 dimensional array alphabetically by
the second field in each row?

Well you could always use usort, which for most purposes should suffice.
However, be aware that this incurs a lot of overhead.

But also remember the words of Donald Knuth: "Premature optimization
is the
root of all evil".

Also, when I use sort() on a two dimensional array, it seems to work a
lot like array_reverse(). Can anyone tell me why?

Works for me. Can you provide a repeatable example?

André Nęss

as well, there is always the bubble sort method.

dunno how that post slipped in there. I hadn't finished typing it yet :/
I hate when stuff like that happens happens :(

Jul 17 '05 #5
Terence:
lawrence wrote:
Is there an easy way to sort a 2 dimensional array alphabetically by
the second field in each row?

Also, when I use sort() on a two dimensional array, it seems to work a
lot like array_reverse(). Can anyone tell me why?

You can always resort to using a bubble sort when sorting requirements
become, er, different.

Be aware that bubble sort is generally considdered an expensive way to
do things.

Well as long as you have usort() I don't see the point. Why specify the
entire algorithm when all you really need to do is specify the comparison
function?

André Nęss

Jul 17 '05 #6
lawrence wrote:
Is there an easy way to sort a 2 dimensional array alphabetically by
the second field in each row?

Also, when I use sort() on a two dimensional array, it seems to work a
lot like array_reverse(). Can anyone tell me why?

array_multisort is the function for multi-dimensional arrays.
--
Bob
London, UK
echo Mail fefsensmrrjyaheeoceoq\! | tr "jefroq\!" "@obe.uk"
Jul 17 '05 #7
André Nęss <an*********************@ifi.uio.no> wrote in message news:<bp**********@maud.ifi.uio.no>...
Terence:
lawrence wrote:
Is there an easy way to sort a 2 dimensional array alphabetically by
the second field in each row?

Also, when I use sort() on a two dimensional array, it seems to work a
lot like array_reverse(). Can anyone tell me why?

You can always resort to using a bubble sort when sorting requirements
become, er, different.

Be aware that bubble sort is generally considdered an expensive way to
do things.

Well as long as you have usort() I don't see the point. Why specify the
entire algorithm when all you really need to do is specify the comparison
function?

André Nęss

I started trying to work it out, but in the end I couldn't. I can
figure out how to sort a one dimensional array, but not a two
dimensional array.

This is as close as I got:
\$twoDArray;// already given a 2 d array
\$newArray = array(); // to store the new, sorted array

for (\$i=0; \$i < count(\$twoDArray); \$i++) {
\$row = \$twoDArray[\$i];
\$r = \$i + 1;
if (\$yesOrNo == 1) \$newArray[] = \$row;
}
function cmp(\$a, \$b) {
if (\$a == \$b) {
return 0;
}
return (\$a > \$b) ? -1 : 1;
}
This would, I think, arrange the rows by the ASCII value of the first
character of the second field in each row, which still isn't
alphabetical, but it is closer.
?>
Jul 17 '05 #8
lawrence:
André Nęss <an*********************@ifi.uio.no> wrote in message
news:<bp**********@maud.ifi.uio.no>...
Terence:
> lawrence wrote:
>
>> Is there an easy way to sort a 2 dimensional array alphabetically by
>> the second field in each row?
>>
>> Also, when I use sort() on a two dimensional array, it seems to work a
>> lot like array_reverse(). Can anyone tell me why?
>
> You can always resort to using a bubble sort when sorting requirements
> become, er, different.
>
> Be aware that bubble sort is generally considdered an expensive way to
> do things.

Well as long as you have usort() I don't see the point. Why specify the
entire algorithm when all you really need to do is specify the comparison
function?

André Nęss

I started trying to work it out, but in the end I couldn't. I can
figure out how to sort a one dimensional array, but not a two
dimensional array.

You original problem statement was to sort a 2-D array by it's second field.
So if you have:

\$data = array(array('value', 'b'), array('value', 'a'));

The second field is thus the 'b' and the 'a'.

So you define the compare function like:
function cmp(\$a, \$b) {
if(\$a[1] == \$b[1]) { return 0; }
return (\$a[1] < \$b[1]) ? -1 : 1;
}

And use
usort(\$data, 'cmp');

\$data should now be:

array(array('value', 'a'), array('value', 'b'))

Remember that what the cmp function receives is two and two elements of
\$data. Each element of \$data is an array, and from these arrays you want to
compare the second element of each to eachother.

If on the other hand all you want is to extract the second field of the 2-D
array and sort these values, you can just iterate over the array,
extracting the second field to a new array, and then sort the resulting
array.

André Nęss
Jul 17 '05 #9
André Nęss <an*********************@ifi.uio.no> wrote in message news:<bp**********@maud.ifi.uio.no>...
lawrence:
André Nęss <an*********************@ifi.uio.no> wrote in message
news:<bp**********@maud.ifi.uio.no>...
Terence:

> lawrence wrote:
>
>> Is there an easy way to sort a 2 dimensional array alphabetically by
>> the second field in each row?
>>
>> Also, when I use sort() on a two dimensional array, it seems to work a
>> lot like array_reverse(). Can anyone tell me why?
>
> You can always resort to using a bubble sort when sorting requirements
> become, er, different.
>
> Be aware that bubble sort is generally considdered an expensive way to
> do things.

Well as long as you have usort() I don't see the point. Why specify the
entire algorithm when all you really need to do is specify the comparison
function?

André Nęss

I started trying to work it out, but in the end I couldn't. I can
figure out how to sort a one dimensional array, but not a two
dimensional array.

You original problem statement was to sort a 2-D array by it's second field.
So if you have:

\$data = array(array('value', 'b'), array('value', 'a'));

The second field is thus the 'b' and the 'a'.

So you define the compare function like:
function cmp(\$a, \$b) {
if(\$a[1] == \$b[1]) { return 0; }
return (\$a[1] < \$b[1]) ? -1 : 1;
}

And use
usort(\$data, 'cmp');

\$data should now be:

array(array('value', 'a'), array('value', 'b'))

Remember that what the cmp function receives is two and two elements of
\$data. Each element of \$data is an array, and from these arrays you want to
compare the second element of each to eachother.

If on the other hand all you want is to extract the second field of the 2-D
array and sort these values, you can just iterate over the array,
extracting the second field to a new array, and then sort the resulting
array.

André Nęss

Thank you, that was perfect.
Jul 17 '05 #10

### This discussion thread is closed

Replies have been disabled for this discussion.