473,503 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

the usort 's puzzle

<?php
$mix=array(
array("A",10),
array("B",5),
array("C",100)
);
function com($x,$y){
echo $x[0];
}
usort($mix,'com');
?>

i think the $x[0] result is A,
but the final $x[0] result is BC.
why???

Sep 30 '07 #1
3 1244
..oO(yo******@gmail.com)
><?php
$mix=array(
array("A",10),
array("B",5),
array("C",100)
);
function com($x,$y){
echo $x[0];
}
usort($mix,'com');
?>

i think the $x[0] result is A,
but the final $x[0] result is BC.
why???
What are you trying to do? The above doesn't really makes sense.
The result depends on what sorting algorithm PHP uses internally.

Micha
Sep 30 '07 #2

<yo******@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...
<?php
$mix=array(
array("A",10),
array("B",5),
array("C",100)
);
function com($x,$y){
echo $x[0];
}
usort($mix,'com');
?>

i think the $x[0] result is A,
but the final $x[0] result is BC.
why???
From www.php.net:
The comparison function must return an integer less than, equal to, or
greater than zero if the first argument is considered to be respectively
less than, equal to, or greater than the second.

So, what does "com" return?

Shelly
Sep 30 '07 #3
On Sun, 30 Sep 2007 05:37:16 +0200, <yo******@gmail.comwrote:
<?php
$mix=array(
array("A",10),
array("B",5),
array("C",100)
);
function com($x,$y){
echo $x[0];
}
usort($mix,'com');
?>

i think the $x[0] result is A,
but the final $x[0] result is BC.
why???
Because PHP uses an internal construct to sort, starting whereever it's
internal functionality is written to start, which should not matter. If it
compares (B,A) & (C,B), it knows enough to sort.
--
Rik Wasmus
Sep 30 '07 #4

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

Similar topics

0
1301
by: Phil Powell | last post by:
I am having to sort an array of objects by an arbitrary object field name that you are not going to know in advance, therefore, I cannot name the sorting function accordingly nor reference the...
0
1613
by: Spidah | last post by:
I am having trouble using the usort function inside a class. Is this possible and if so what is the correct way to pass the compare function to it? I assume this function must also be inside the...
5
2951
by: Alan Little | last post by:
I wrote a sorting function to use with usort(). It worked fine, and I went on with the development of my code. At one point I changed the name of the sorting function, but forgot to update it in...
1
13074
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of...
0
2002
by: xavier vazquez | last post by:
have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the...
5
4447
by: ashish0799 | last post by:
HI I M ASHISH I WANT ALGORYTHMUS OF THIS PROBLEM Jigsaw puzzles. You would have solved many in your childhood and many people still like it in their old ages also. Now what you have got to do...
3
3194
by: oncue01 | last post by:
Word Puzzle Task You are going to search M words in an N × N puzzle. The words may have been placed in one of the four directions as from (i) left to right (E), (ii) right to left (W), (iii) up...
1
1576
by: ojsimon | last post by:
Hi I Am using Simplepie to display some rss feeds, currently it displays them in date and time order but i want it to display them in relevance order by using a usort function. here is the code...
4
19760
by: honey777 | last post by:
Problem: 15 Puzzle This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is an example of the puzzle: The goal is to...
0
7192
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
7064
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
7261
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
7445
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
5559
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,...
1
4991
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...
0
4665
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1492
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 ...
0
369
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...

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.