PHP-更改字符排序顺序,例如;a「&燃气轮机&引用;{";=True
我试图定制PHP的PHP-更改字符排序顺序,例如;a「&燃气轮机&引用;{";=True,php,sorting,Php,Sorting,我试图定制PHP的usort函数来更改字符排序顺序 目前,只有“*”符号字符等同于具有小于字母字符的值,例如“a”,即“”*“
usort
函数来更改字符排序顺序
目前,只有“*
”符号字符等同于具有小于字母字符的值,例如“a
”,即“”*“
。其他符号,例如“{
”,具有大于字母的值,例如“a
”,即“{a”=FALSE
我想进行排序,使值“{*}
”位于已排序数组的顶部,就像值是“*
”。这是我当前使用的函数,用于根据对象的多个属性对对象数组进行排序。[属性:这是usort
上的代码的修改版本]
如果一个
usort
,它按照您定义的顺序对某些字符进行优先级排序,然后使用常规的strcmp
大概是这样的:
<?php
$list = [ 'abc',
'{a}',
'*',
'{*}',
'{abc',
'}a',
]; // An array of strings to sort
$customOrderPrioritized = ['{','*','}']; // Add any characters here to prioritize them, in the order they appear in this array
function ustrcmp($a, $b, $customOrderPrioritized) {
if ($a === $b) return -1; // same, doesn't matter who goes first
$n = min(strlen($a), strlen($b)); // compare up to the length of the shortest string
for ($i = 0; $i < $n; $i++) {
if ($a[$i] === $b[$i]) continue; // matching character, continue to compare next
$a_prio = in_array($a[$i], $customOrderPrioritized);
$b_prio = in_array($b[$i], $customOrderPrioritized);
// If either one has a prioritized char...
if ($a_prio || $b_prio) {
if ($a_prio && $b_prio) {
// Both are prioritized, check which one is first...
return array_search($a[$i], $customOrderPrioritized) <= array_search($b[$i], $customOrderPrioritized) ? -1 : 1;
} elseif ($a_prio) {
return -1; // a < b
} else { // must be $b_prio
return +1; // b > a
}
}
return strcmp($a[i], $b[$i]); // compare single character
}
// if they have identical beginning, shortest should be first
return strlen($a) <= strlen($b) ? -1 : +1;
}
usort(
$list,
function($a, $b) use ($customOrderPrioritized){
return ustrcmp($a, $b, $customOrderPrioritized);
}
); // run custom comparison function, pass in $customOrderPrioritized (or make it global)
print_r($list); // print the list
/* The sorted array should look like:
Array
(
[0] => {*}
[1] => {a}
[2] => {abc
[3] => *
[4] => }a
[5] => abc
)
*/
<?php
$list = [ 'abc',
'{a}',
'*',
'{*}',
'{abc',
'}a',
]; // An array of strings to sort
$customOrderPrioritized = ['{','*','}']; // Add any characters here to prioritize them, in the order they appear in this array
function ustrcmp($a, $b, $customOrderPrioritized) {
if ($a === $b) return -1; // same, doesn't matter who goes first
$n = min(strlen($a), strlen($b)); // compare up to the length of the shortest string
for ($i = 0; $i < $n; $i++) {
if ($a[$i] === $b[$i]) continue; // matching character, continue to compare next
$a_prio = in_array($a[$i], $customOrderPrioritized);
$b_prio = in_array($b[$i], $customOrderPrioritized);
// If either one has a prioritized char...
if ($a_prio || $b_prio) {
if ($a_prio && $b_prio) {
// Both are prioritized, check which one is first...
return array_search($a[$i], $customOrderPrioritized) <= array_search($b[$i], $customOrderPrioritized) ? -1 : 1;
} elseif ($a_prio) {
return -1; // a < b
} else { // must be $b_prio
return +1; // b > a
}
}
return strcmp($a[i], $b[$i]); // compare single character
}
// if they have identical beginning, shortest should be first
return strlen($a) <= strlen($b) ? -1 : +1;
}
usort(
$list,
function($a, $b) use ($customOrderPrioritized){
return ustrcmp($a, $b, $customOrderPrioritized);
}
); // run custom comparison function, pass in $customOrderPrioritized (or make it global)
print_r($list); // print the list
/* The sorted array should look like:
Array
(
[0] => {*}
[1] => {a}
[2] => {abc
[3] => *
[4] => }a
[5] => abc
)
*/