Skip to content

Commit bfc89e6

Browse files
committed
Add extra example for Collator::getSortKey
1 parent bb4abab commit bfc89e6

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

reference/intl/collator/get-sort-key.xml

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
&reftitle.examples;
6767
<para>
6868
<example>
69-
<title><function>collator_get_sort_key</function>example</title>
69+
<title><function>collator_get_sort_key</function> example</title>
7070
<programlisting role="php">
7171
<![CDATA[
7272
<?php
@@ -85,6 +85,76 @@ echo bin2hex($res);
8585
</screen>
8686
</example>
8787
</para>
88+
89+
<para>
90+
<example>
91+
<title><function>Collator::getSortKey</function> example with <function>usort</function></title>
92+
<programlisting role="php">
93+
<![CDATA[
94+
<?php
95+
$data = [
96+
[ 'name' => '🇳🇱 Derick Rethans', 'linked_account' => 'https://phpc.social/users/derickr' ],
97+
[ 'name' => 'Elephpant', 'linked_account' => 'https://phpc.social/phpc' ],
98+
[ 'name' => '🇫🇷 Marcus Bointon', 'linked_account' => 'https://phpc.social/users/Synchro' ],
99+
];
100+
101+
/* Create the collator */
102+
$col = new Collator('en');
103+
104+
/* Sort upper-case letters before lower-case letters */
105+
$col->setAttribute(Collator::CASE_FIRST, Collator::UPPER_FIRST);
106+
107+
/* Use a user-defined function with sort, that strips out the emojis */
108+
*/
109+
usort(
110+
$data,
111+
function($a, $b) use ($col) {
112+
/* Remove the character class 'S' (the Symbols), and remove whitespace
113+
* (with trim) */
114+
$aName = trim(preg_replace('/\p{S}+/u', '', $a['name']));
115+
$bName = trim(preg_replace('/\p{S}+/u', '', $b['name']));
116+
117+
/* Create the sort key */
118+
$aKey = $col->getSortKey($aName);
119+
$bKey = $col->getSortKey($bName);
120+
121+
/* Use the sort key to signal which element sorts first */
122+
return $aKey <=> $bKey;
123+
}
124+
);
125+
126+
var_dump($data);
127+
?>
128+
]]>
129+
</programlisting>
130+
&example.outputs.similar;
131+
<screen>
132+
array(3) {
133+
[0] =>
134+
array(2) {
135+
'name' =>
136+
string(25) "🇳🇱 Derick Rethans"
137+
'linked_account' =>
138+
string(33) "https://phpc.social/users/derickr"
139+
}
140+
[1] =>
141+
array(2) {
142+
'name' =>
143+
string(9) "Elephpant"
144+
'linked_account' =>
145+
string(24) "https://phpc.social/phpc"
146+
}
147+
[2] =>
148+
array(2) {
149+
'name' =>
150+
string(25) "🇫🇷 Marcus Bointon"
151+
'linked_account' =>
152+
string(33) "https://phpc.social/users/Synchro"
153+
}
154+
}
155+
</screen>
156+
</example>
157+
</para>
88158
</refsect1>
89159

90160
<refsect1 role="seealso">

0 commit comments

Comments
 (0)