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

sort multi-dimensional array

Hi,

does anyone know how i can sort a multi-dimensional array
by a specific field ?

for example i want to sort arr[m][n] where n=2,
but i need the data of every array to follow that order.

example array:
arr[0][0]
arr[0][1]
arr[0][2]
arr[0][3]
arr[0][4]
arr[0][5]

arr[1][0]
arr[1][1]
arr[1][2]
arr[1][3]
arr[1][4]
arr[1][5]
Aug 25 '06 #1
4 3006
Balaskas Evaggelos wrote:
Hi,

does anyone know how i can sort a multi-dimensional array
by a specific field ?

for example i want to sort arr[m][n] where n=2,
but i need the data of every array to follow that order.

example array:
arr[0][0]
arr[0][1]
arr[0][2]
arr[0][3]
arr[0][4]
arr[0][5]

arr[1][0]
arr[1][1]
arr[1][2]
arr[1][3]
arr[1][4]
arr[1][5]
You can use the array_multisort() function to do this. More info and a
number of examples of useage can be found at :
http://uk.php.net/manual/en/function...-multisort.php

Aug 25 '06 #2
gb*******@googlemail.com wrote:
Balaskas Evaggelos wrote:
>Hi,

does anyone know how i can sort a multi-dimensional array
by a specific field ?

for example i want to sort arr[m][n] where n=2,
but i need the data of every array to follow that order.

example array:
arr[0][0]
arr[0][1]
arr[0][2]
arr[0][3]
arr[0][4]
arr[0][5]

arr[1][0]
arr[1][1]
arr[1][2]
arr[1][3]
arr[1][4]
arr[1][5]

You can use the array_multisort() function to do this. More info and a
number of examples of useage can be found at :
http://uk.php.net/manual/en/function...-multisort.php
thanks for your anwser but the first thing i checked,
before i wrote this post, was that url.

The example are for multi-dimensional array of one-dimensional arrays.

I can't find how i can sort a multi-dimensional array of array[m][n]
and for a specific field example n=2.
Aug 25 '06 #3
Balaskas Evaggelos wrote:
thanks for your anwser but the first thing i checked,
before i wrote this post, was that url.

The example are for multi-dimensional array of one-dimensional arrays.

I can't find how i can sort a multi-dimensional array of array[m][n]
and for a specific field example n=2.
Maybe I just am not understanding you correctly, but aren't they the
same thing?
In PHP:
$ar['m']['n'] = 1;
is equivalent to:
$ar = array('m' =array('n' =1));

Both can be referenced as $ar['m']['n'].

Off the top of my head, the most relevant example from that php manual
page would be Example 3. Sorting database results. It sounds like you
want to essentially sort a 'table' of data by one of the columns. The
concepts of that example should point you in the right direction.
Unless, as I said, I am completely misunderstanding your question.

Aug 25 '06 #4
mo*******************@yahoo.com wrote:
Balaskas Evaggelos wrote:
>thanks for your anwser but the first thing i checked,
before i wrote this post, was that url.

The example are for multi-dimensional array of one-dimensional arrays.

I can't find how i can sort a multi-dimensional array of array[m][n]
and for a specific field example n=2.

Maybe I just am not understanding you correctly, but aren't they the
same thing?
In PHP:
$ar['m']['n'] = 1;
is equivalent to:
$ar = array('m' =array('n' =1));

Both can be referenced as $ar['m']['n'].

Off the top of my head, the most relevant example from that php manual
page would be Example 3. Sorting database results. It sounds like you
want to essentially sort a 'table' of data by one of the columns. The
concepts of that example should point you in the right direction.
Unless, as I said, I am completely misunderstanding your question.
maybe i don't write it correctly - bad english :)

in the example 3 the multi-dimensional array breaks into two
one-dimensional arrays to sort them. So the results is
sorted for every separated array.

i need the 2 arrays to be sorted only by the second field of the second
array. And all the rest to be sorted according by this field.

this is my array:

arr[0][0]
arr[0][1]
arr[0][2]
arr[0][3]
arr[0][4]
arr[0][5]

arr[1][0]
arr[1][1]
arr[1][2]
arr[1][3]
arr[1][4]
arr[1][5]

....
....

i want to sort the arr be second element into the arr:

arr[m][2], and everything else will follow this order.

---
as m: 0,1,2,3,4,5,6,7,8,9,...
as n: 0,1,2,3,4,5,6,7,8,9,...
Aug 25 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: mikeswk | last post by:
Hi, all I'm using jsp to connect to mysql 3.23.44 max using connector 3.0.6 (default charset=latin1) I'm trying to put some multi-byte characters (for example big5) together with latin...
5
by: Dr. Ann Huxtable | last post by:
Hello All, I am reading a CSV (comma seperated value) file into a 2D array. I want to be able to sort multiple columns (ala Excel), so I know for starters, I cant be using the array, I need...
4
by: P Adhia | last post by:
Hello, If the explain shows that DB2 needs to sort the result of a cursor, does that always happen? i.e. if the resultset of the cursor is empty, does the sort have any overhead? It appears...
2
by: Tim Marshall | last post by:
The following is happening in two instances in A2003. The one I s\describe is the easiest one. Theme controls and autocorrupt have been turned off long ago. An unbound main form with a list...
26
by: Daron | last post by:
How do I sort a table, from a form. I can't use SQL! WE have an Access application that is created by an outside vendor. The one form states: "Please make sure all records in table are sorted by...
18
by: xahlee | last post by:
Last year, i've posted a tutorial and commentary about Python and Perl's sort function. (http://xahlee.org/perl-python/sort_list.html) In that article, i discussed a technique known among...
0
by: SolomonShavitzMetsFan | last post by:
Is there a way to have a button above each of three columns of a subform to do a runtime sort of the respective columns below the buttons? The only way I can think of is to have three different...
1
by: flyfast81 | last post by:
I am trying to create a multi-threaded bubblesort but continue to get nothing. Here is my code, can someone help. Thanks #include <stdio.h> #include <stdlib.h> #include <pthread.h> void...
4
by: jehugaleahsa | last post by:
Hello: I want to know the best way to sort a BindingList without needed to turn it into a DataTable. Of course, my BindingList is being used indirectly by a BindingSource. So, how do I sort a...
4
by: cici | last post by:
I am having trouble sorting secondary keys in a multi-level hash. I have a sample program that exhibits the problem...the following code is sorting New 6 Month before New 1 Month. What am I doing...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.