473,402 Members | 2,061 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,402 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 17591
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.