473,399 Members | 3,401 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,399 software developers and data experts.

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 2313
On Mar 27, 5:20 pm, Schraalhans Keukenmeester <bitbuc...@invalid.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($dircontents, 'cmp');

Mar 28 '07 #2
ZeldorBlat wrote:
On Mar 27, 5:20 pm, Schraalhans Keukenmeester <bitbuc...@invalid.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($dircontents, '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...@invalid.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($dircontents, '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
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
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...
3
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...
5
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...
11
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...
1
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...
0
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...
1
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
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.