-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy patha_sorted.php
119 lines (91 loc) · 3.77 KB
/
a_sorted.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
require_once __DIR__ . '/../autoload.php';
use function nspl\a\sorted;
use function nspl\op\itemGetter;
use function nspl\op\propertyGetter;
use function nspl\op\methodCaller;
// 1. Sort array
$sorted = sorted([3, 1, 2]);
display('1. Sorted array', $sorted);
// 2. Sort array in descending order
$sortedDesc = sorted([3, 1, 2], true);
display('2. Sorted in descending order', $sortedDesc);
// 3. Sort array by the result of a given function (order words by length)
$sortedByLength = sorted(['bc', 'a', 'abc'], 'strlen');
display('3.1. Sorted by length', $sortedByLength);
$sortedByLengthDesc = sorted(['bc', 'a', 'abc'], true, 'strlen');
display('3.2. Sorted by length in descending order', $sortedByLengthDesc);
// 4. Sort array by the result of user-defined function (order word by the 1st character)
$sortedByTheFirstCharacter = sorted(['bc', 'a', 'abc'], function($v) { return $v[0]; });
display('4. Sorted by the 1st character', $sortedByTheFirstCharacter);
// 5. Which is the same as
$sortedByTheFirstCharacter = sorted(['bc', 'a', 'abc'], itemGetter(0));
display('5.1. Sorted by the 1st character with nspl\op\itemGetter', $sortedByTheFirstCharacter);
$sortedByTheFirstCharacterDesc = sorted(['bc', 'a', 'abc'], true, itemGetter(0));
display('5.2. Sorted by the 1st character with nspl\op\itemGetter in descending order', $sortedByTheFirstCharacterDesc);
// 6. Sort array with comparison function (order word by the 1st character)
$sortedByTheFirstCharacter = sorted(['bc', 'a', 'abc'], false, null, function($v1, $v2) {
return chr($v1[0]) - chr($v2[0]);
});
display('6. Sorted by the 1st character with a comparison function', $sortedByTheFirstCharacter);
// 7. Sort list of strings lexicographically
$sortedLexicographically = sorted(['bc', 'a', 'abc'], false, null, 'strcmp');
display('7. Sorted lexicographically', $sortedLexicographically);
// 8. Sort multidimensional array (sort list of users by their names)
$users = [
array('name' => 'Robert', 'age' => 20),
array('name' => 'Alex', 'age' => 30),
array('name' => 'Jack', 'age' => 25),
];
$sortedByName = sorted($users, itemGetter('name'));
display('8.1. Users sorted by name', $sortedByName);
$sortedByNameDesc = sorted($users, true, itemGetter('name'));
display('8.2. Users sorted by name in descending order', $sortedByNameDesc);
// 9. Sort list of objects by property value (sort list of users by their name)
$users = [
new User('Robert', 20),
new User('Alex', 30),
new User('Jack', 25),
];
$sortedByName = sorted($users, propertyGetter('name'));
display('9.1. Users presented as list of objects sorted by name', $sortedByName);
$sortedByNameDesc = sorted($users, true, propertyGetter('name'));
display('9.2. Users presented as list of objects sorted by name in descending order', $sortedByNameDesc);
// 10. Sort list of objects by method result (sort list of users by their age)
$sortedByAge = sorted($users, methodCaller('getAge'));
display('10.1. Users presented as list of objects sorted by age', $sortedByAge);
$sortedByAgeDesc = sorted($users, true, methodCaller('getAge'));
display('10.2. Users presented as list of objects sorted by age in descending order', $sortedByAgeDesc);
class User
{
public $name;
public $age;
public function __construct($name, $age)
{
$this->name = $name;
$this->age = $age;
}
public function getName()
{
return $this->name;
}
public function getAge()
{
return $this->age;
}
public function __toString()
{
return $this->name . ', ' . $this->age . 'y.o.';
}
}
function display($title, array $array)
{
echo $title . ":\n";
foreach ($array as $item) {
if (is_array($item)) {
$item = json_encode($item);
}
echo ' - ' . $item . "\n";
}
echo "\n";
}