473,545 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

array_multisort ()

I'm having a hard time getting to grips with this function.

I have an array shaped like this:

$dircontents[0]['filename'] ='index.html'
$dircontents[0]['owner'] = 'www'
$dircontents[0]['group'] = 'www'
$dircontents[0]['type'] = 'file'

$dircontents[1]['filename'] ='images'
$dircontents[1]['owner'] = 'www'
$dircontents[1]['group'] = 'www'
$dircontents[1]['type'] = 'dir'

etc.

'type' can be any of {dir,file,link}

How do I get the array sorted, alphabetically, first by type, then filename?
My attempts sofar have lead to a seemingly unsorted array, not even
close to anything recognizable. Is array_multisort () even what I should
be looking for?

Help appreciated!
Sh.
Mar 27 '07 #1
3 2320
On Mar 27, 5:20 pm, Schraalhans Keukenmeester <bitbuc...@inva lid.spam>
wrote:
I'm having a hard time getting to grips with this function.

I have an array shaped like this:

$dircontents[0]['filename'] ='index.html'
$dircontents[0]['owner'] = 'www'
$dircontents[0]['group'] = 'www'
$dircontents[0]['type'] = 'file'

$dircontents[1]['filename'] ='images'
$dircontents[1]['owner'] = 'www'
$dircontents[1]['group'] = 'www'
$dircontents[1]['type'] = 'dir'

etc.

'type' can be any of {dir,file,link}

How do I get the array sorted, alphabetically, first by type, then filename?
My attempts sofar have lead to a seemingly unsorted array, not even
close to anything recognizable. Is array_multisort () even what I should
be looking for?

Help appreciated!
Sh.
Try usort() instead:

function cmp($a, $b) {
if($a['type'] < $b['type'])
return -1;
elseif($a['type'] $b['type'])
return 1;
elseif($a['filename'] < $b['filename'])
return -1;
elseif($a['filename'] $b['filename'])
return 1;
else
return 0;
}

usort($dirconte nts, 'cmp');

Mar 28 '07 #2
ZeldorBlat wrote:
On Mar 27, 5:20 pm, Schraalhans Keukenmeester <bitbuc...@inva lid.spam>
wrote:
>I'm having a hard time getting to grips with this function.

I have an array shaped like this:

$dircontents[0]['filename'] ='index.html'
$dircontents[0]['owner'] = 'www'
$dircontents[0]['group'] = 'www'
$dircontents[0]['type'] = 'file'

$dircontents[1]['filename'] ='images'
$dircontents[1]['owner'] = 'www'
$dircontents[1]['group'] = 'www'
$dircontents[1]['type'] = 'dir'

etc.

'type' can be any of {dir,file,link}

How do I get the array sorted, alphabetically, first by type, then filename?
My attempts sofar have lead to a seemingly unsorted array, not even
close to anything recognizable. Is array_multisort () even what I should
be looking for?

Help appreciated!
Sh.

Try usort() instead:

function cmp($a, $b) {
if($a['type'] < $b['type'])
return -1;
elseif($a['type'] $b['type'])
return 1;
elseif($a['filename'] < $b['filename'])
return -1;
elseif($a['filename'] $b['filename'])
return 1;
else
return 0;
}

usort($dirconte nts, 'cmp');
Great! That's what I needed indeed. Somewhere in the back of my head...
Thanx Zeldor!
Mar 28 '07 #3
Schraalhans Keukenmeester wrote:
ZeldorBlat wrote:
>On Mar 27, 5:20 pm, Schraalhans Keukenmeester <bitbuc...@inva lid.spam>
wrote:
>>I'm having a hard time getting to grips with this function.

I have an array shaped like this:

$dircontent s[0]['filename'] ='index.html'
$dircontent s[0]['owner'] = 'www'
$dircontent s[0]['group'] = 'www'
$dircontent s[0]['type'] = 'file'

$dircontent s[1]['filename'] ='images'
$dircontent s[1]['owner'] = 'www'
$dircontent s[1]['group'] = 'www'
$dircontent s[1]['type'] = 'dir'

etc.

'type' can be any of {dir,file,link}

How do I get the array sorted, alphabetically, first by type, then filename?
My attempts sofar have lead to a seemingly unsorted array, not even
close to anything recognizable. Is array_multisort () even what I should
be looking for?

Help appreciated!
Sh.
Try usort() instead:

function cmp($a, $b) {
if($a['type'] < $b['type'])
return -1;
elseif($a['type'] $b['type'])
return 1;
elseif($a['filename'] < $b['filename'])
return -1;
elseif($a['filename'] $b['filename'])
return 1;
else
return 0;
}

usort($dircont ents, 'cmp');
Great! That's what I needed indeed. Somewhere in the back of my head...
Thanx Zeldor!
Had to rewrite it a little since I usesd strings. Final version, in case
someone else likes to use it as well:

function cmp($a, $b) {
$retval = strcmp($a['type'],$b['type']);
if ($retval === 0)
$retval = strcmp($a['filename'],$b['filename']);
return $retval;
}
usort($this->dircontents, 'cmp');

Again, thanks for the pointer!
Mar 28 '07 #4

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

Similar topics

1
2643
by: Ward Germonpé | last post by:
Hi, I have a table loaded in an array like so : $table $table $table $table $table $table $table $table $table ...... now I want to sort this table using the ID column.
2
2713
by: michael nieuwenhuizen | last post by:
<newbie alert> i have 2 related arrays that i wanna sort at once. if i try: print_r($my_array1); array_multisort($my_array1, SORT_ASC, $my_array2, SORT_ASC); print_r($my_array1); both arrays remain unsorted. if i try:
3
2310
by: nate | last post by:
i'm sorting 2 arrays together. so i have a group of stats in one array and their corresponding owners in the other array. and it sorts them correctly. however, i'm wondering how i can resolve ties in a random way. is there a way to do this or do i have to write the multisort function myself. and if i do, what's the most efficient way to do...
5
2826
by: Frostillicus | last post by:
I'm trying to use array_multisort to sort by one of the dimensions of an array stored in $GLOBALS like this: array_multisort($GLOBALS, SORT_STRING, SORT_DESC); Each "row" in $GLOBALS contains a value for 'href', 'desc', and 'info' but when I try to pass one of these "columns" to array_multisort() it whinges about it not being an array or...
11
2075
by: Alan Searle | last post by:
I have been trying to get an array to sort on a particular column and have been having problems with my main key ... array_multisort($dirlist, SORT_DESC, SORT_STRING); This function takes an array ($dirlist) which has been loaded with a directory listing where each directory has its own index number ("dirid") and displays the listing in...
1
1624
by: Abhi | last post by:
Hi! I am wondering if someone can point me to the needed sort function. There are so many of them that I simply got lost by reading their descriptions. I try to explain what I need. I have a multidimensional array and I have a function that outputs it as a table. $arr contains an array that contains usernames $arr contains an array that...
0
1281
by: R | last post by:
Hi All, I have simple question can array_multisort be made locale based sort for SORT_STRING flag? or is there a work around to force strcoll when sorting strings? thanks in advance for any help best regards R
1
1362
by: luftikus143 | last post by:
Hi, I have an array stocking statistical data as follow: $record = 2039; $record = 948; $record = 3621; $record = 52; $record = 575; $record = 83;
5
1502
by: Anthony2oo5 | last post by:
Hello people, just a quick question I have here. My array looks like this: Array ( => Array ( => 15 => example.com => examplecom => NshKsi23
0
7680
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7934
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7446
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6003
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5349
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3476
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3459
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
731
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.