bonjour

j'ai un souci de compréhension de usort

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
function cmp($a, $b) {
echo $a.'-'.$b.'<br>';
if (strlen($a) < strlen($b)) {
return -1 ;
} elseif (strlen($a) == strlen($b)) {
return 0 ;
} else {
return 1 ;
}
}
$fruits[0] = 'a';
$fruits[1] = 'b';
$fruits[2] = 'c';
$fruits[3] = 'e';
$fruits[4] = 'f';
usort($fruits, 'cmp');
foreach($fruits as $cle => $valeur) {
echo "$valeur, ";
}
ce code affiche :

c-b
f-c
c-a
e-c
f-e
b-a
f, e, c, b, a,

Je comprend pas pourquoi la comparaison commence avec c-b ?