-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathch4p4sortingarrays.php
77 lines (73 loc) · 1.44 KB
/
ch4p4sortingarrays.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
*
* Created by PhpStorm.
* User: Tim
* Date: 6-6-2015
* Time: 12:32
*/
include_once('generalIncludes.php');
echo '<input id="chapter" type="hidden" value="4">';
echo '<h2>Chapter 4 - paragraph Sorting Arrays</h2>';
echo 'sort()';
showcode (<<<'CODE'
$array = array('a' => 'foo', 'b' => 'bar', 'c' => 'baz');
sort($array);
var_dump($array);
CODE
);
echo 'asort()';
showcode (<<<'CODE'
$array = array('a' => 'foo', 'b' => 'bar', 'c' => 'baz');
asort($array);
var_dump($array);
CODE
);
echo 'natsort()';
showcode (<<<'CODE'
$array = array('10t', '2t', '3t');
natsort($array);
var_dump($array);
CODE
);
echo 'ksort()';
showcode (<<<'CODE'
$a = array('a' => 30, 'b' => 10, 'c' => 22);
ksort($a);
var_dump($a);
CODE
);
echo'Listing 4.11: User-defined comparison';
showcode (<<<'CODE'
function myCmp($left, $right) {
// Sort according to the length of the value.
// If the length is the same, sort normally
$diff = strlen($left) - strlen($right);
if (!$diff) {
return strcmp($left, $right);
}
return $diff;
}
$a = array(
'three',
'2two',
'one',
'two'
);
usort($a, 'myCmp');
var_dump($a);
CODE
);
echo'Listing 4.12: Reversing sort order';
showcode (<<<'CODE'
function myCmp($left, $right) {
// Reverse-sort according to the length of the value.
// If the length is the same, sort normally
$diff = strlen($right) - strlen($left);
if (!$diff) {
return strcmp($right, $left);
}
return $diff;
}
CODE
);