先按字母排序再按数字排序 Laravel 集合
Sort By Alphabet then Numbers Laravel Collection
我正在寻找一种方法来对集合进行排序,使以字母开头的名字排在最前面,然后是以数字开头的名字。例如:
$collection = collect([
['name' => 'b', 'symbol' => '#'],
['name' => '2a', 'symbol' => '$'],
['name' => '1', 'symbol' => '@'],
['name' => 'a', 'symbol' => '%']
]);
上面的集合应该这样排序:
[
[
"name" => "a",
"symbol" => "%",
],
[
"name" => "b",
"symbol" => "#",
],
[
"name" => "1",
"symbol" => "@",
],
[
"name" => "2a",
"symbol" => "$",
],
]
但这是我使用 sortBy
方法排序时得到的结果:
$collection->sortBy('name')->values()->all();
[
[
"name" => "1",
"symbol" => "@",
],
[
"name" => "2a",
"symbol" => "$",
],
[
"name" => "a",
"symbol" => "%",
],
[
"name" => "b",
"symbol" => "#",
],
]
知道如何对这个集合进行排序,以便以字母开头的名字排在第一位。谢谢
您需要定义自己的自定义比较器函数来使用 sort
对这些集合对象进行排序。
比较两个名字,检查它们是否都是字母。如果两者都是字母,则通常的字符串比较再次使用 strcasecmp
shall suffice. If either of them is an alphabet, push them to higher ranks by returning value -1
, meaning to be placed above in the sorted order. If both are numerical or alphanumeric, use strcasecmp
。
<?php
$collection = collect([
['name' => 'b', 'symbol' => '#'],
['name' => '2a', 'symbol' => '$'],
['name' => '1', 'symbol' => '@'],
['name' => 'a', 'symbol' => '%']
]);
$collection = $collection->sort(function($a,$b){
$a_is_alphabet = preg_match('/^[a-zA-Z]+$/', $a['name']) === 1;
$b_is_alphabet = preg_match('/^[a-zA-Z]+$/', $b['name']) === 1;
if($a_is_alphabet && $b_is_alphabet){
return strcasecmp($a['name'], $b['name']);
}elseif($a_is_alphabet){
return -1;
}elseif($b_is_alphabet){
return 1;
}
return strcasecmp($a['name'], $b['name']);
});
我正在寻找一种方法来对集合进行排序,使以字母开头的名字排在最前面,然后是以数字开头的名字。例如:
$collection = collect([
['name' => 'b', 'symbol' => '#'],
['name' => '2a', 'symbol' => '$'],
['name' => '1', 'symbol' => '@'],
['name' => 'a', 'symbol' => '%']
]);
上面的集合应该这样排序:
[
[
"name" => "a",
"symbol" => "%",
],
[
"name" => "b",
"symbol" => "#",
],
[
"name" => "1",
"symbol" => "@",
],
[
"name" => "2a",
"symbol" => "$",
],
]
但这是我使用 sortBy
方法排序时得到的结果:
$collection->sortBy('name')->values()->all();
[
[
"name" => "1",
"symbol" => "@",
],
[
"name" => "2a",
"symbol" => "$",
],
[
"name" => "a",
"symbol" => "%",
],
[
"name" => "b",
"symbol" => "#",
],
]
知道如何对这个集合进行排序,以便以字母开头的名字排在第一位。谢谢
您需要定义自己的自定义比较器函数来使用 sort
对这些集合对象进行排序。
比较两个名字,检查它们是否都是字母。如果两者都是字母,则通常的字符串比较再次使用 strcasecmp
shall suffice. If either of them is an alphabet, push them to higher ranks by returning value -1
, meaning to be placed above in the sorted order. If both are numerical or alphanumeric, use strcasecmp
。
<?php
$collection = collect([
['name' => 'b', 'symbol' => '#'],
['name' => '2a', 'symbol' => '$'],
['name' => '1', 'symbol' => '@'],
['name' => 'a', 'symbol' => '%']
]);
$collection = $collection->sort(function($a,$b){
$a_is_alphabet = preg_match('/^[a-zA-Z]+$/', $a['name']) === 1;
$b_is_alphabet = preg_match('/^[a-zA-Z]+$/', $b['name']) === 1;
if($a_is_alphabet && $b_is_alphabet){
return strcasecmp($a['name'], $b['name']);
}elseif($a_is_alphabet){
return -1;
}elseif($b_is_alphabet){
return 1;
}
return strcasecmp($a['name'], $b['name']);
});