By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,529 Members | 864 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,529 IT Pros & Developers. It's quick & easy.

sort 2 dimensional array alphabetically by second field in each row

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a
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

P: n/a
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.

http://www.google.com.au/search?q=%22bubble+sort%22
Jul 17 '05 #4

P: n/a
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 :/
please ignore (see my other post on this thread).
I hate when stuff like that happens happens :(

Jul 17 '05 #5

P: n/a
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

P: n/a
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

P: n/a
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];
$headline1 = $twoDArray[$i][1];
$firstCharacter = $headline1[0];
$r = $i + 1;
$headline2 = $twoDArray[$r][1];
$firstCharacter2 = $headline2[0];
$yesOrNo = cmp($headline1, $headline2);
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

P: n/a
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

P: n/a
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.