473,226 Members | 1,382 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 17578
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 thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 4 by: Todd | last post by: I'm new to c++ and was wondering how to sort a 2 dimensional array. I'm using a select sort for 1 dimensional arrays but it is not working for a 2 dimensional array. The 2 dimensional array are... 0 by: Tony Johansson | last post by: Hello Experts!! I know that you can initialize a two-dimensional array of integer in this way int number = { {1,6,78}, {7,2,23} } Here we have declared a two dimensional array with 2 rows, each... 2 by: Carlos | last post by: I have the following two dimension array: public double mData = new double; now I need to sort the values based on the values of third index of the second dimension, like = 11 = 22 = 444 4 by: entitledX | last post by: Hi, I'm trying to use the HDF library to read a few HDF files that I need to process. The data in each file varies in rows, but the columns remain constant. Because of that, I had dynamically... 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. ... 8 by: per9000 | last post by: Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious... 272 by: Peter Olcott | last post by: http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two... 2 by: moondog | last post by: How do you sort a 2D array? Dim myArray(10000, 9) As String myArray contains 10000 records with 9 fields. I want to sort on the Ninth field. I want to be able to use the sort method, but... 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