I would like to know how to sort numeric characters before a string. I use array_multisort to sort my array.
My sample data is available
$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 sorting. String number "2-01" is sorted 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
],
];
Expected Result. I want to sort the string "2-01", "2-03" after the numbers 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
];