I would like to know how to sort numeric before string. I use array_multisort to sort my array.
There is my sample data
$arr = [
[
'company_name' => '',
'expiryDate' => '2018.7',
'classification' => 1
],
[
'company_name' => '',
'expiryDate' => '2018.7',
'classification' => '2-03'
],
[
'company_name' => '',
'expiryDate' => '2018.7',
'classification' => 3
],
[
'company_name' => '',
'expiryDate' => '2018.7',
'classification' => '2-01'
],
];
The current result after sort. The string numeric '2-01' is sort between 1 and 3
$arr = [
[
'company_name' => '',
'expiryDate' => '2018.7',
'classification' => 1
],
[
'company_name' => '',
'expiryDate' => '2018.7',
'classification' => '2-01'
],
[
'company_name' => '',
'expiryDate' => '2018.7',
'classification' => '2-03'
],
[
'company_name' => '',
'expiryDate' => '2018.7',
'classification' => 3
],
];
My expected result. I want to sort string '2-01', '2-03' after the numeric 1 and 3
$arr = [
[
'company_name' => '',
'expiryDate' => '2018.7',
'classification' => 1
],
[
'company_name' => '',
'expiryDate' => '2018.7',
'classification' => 3
],
[
'company_name' => '',
'expiryDate' => '2018.7',
'classification' => '2-01'
],
[
'company_name' => '',
'expiryDate' => '2018.7',
'classification' => '2-03'
],
];
My sort fields. I used array_multisort to sort multiple fields
$sortFields = [
'classification' => SORT_ASC,
'company_name' => SORT_ASC,
'expiryDate' => SORT_ASC
];
usort($arr, function($a1, $a2) {})