473,226 Members | 1,382 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,226 software developers and data experts.

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.

http://www.google.com.au/search?q=%22bubble+sort%22
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 :/
please ignore (see my other post on this thread).
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];
$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
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<Tinterface and have run into a real roadblack. There does not...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
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: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
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: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
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...

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.