How to sorting data when contained in the underscore ?
Explanation is on the picture,,
Thank you for help.
How to sorting data when contained in the underscore ?
Explanation is on the picture,,
Thank you for help.
You want a natural sort.
<?php
$items =
[
'ABC_1_1',
'ABC_1_3',
'ABC_1_10',
'ABC_1_11',
'ABC_1_5',
'ABC_1_7'
];
sort($items, SORT_NATURAL);
var_dump($items);
Output:
array(6) {
[0]=>
string(7) "ABC_1_1"
[1]=>
string(7) "ABC_1_3"
[2]=>
string(7) "ABC_1_5"
[3]=>
string(7) "ABC_1_7"
[4]=>
string(8) "ABC_1_10"
[5]=>
string(8) "ABC_1_11"
}
You can try below -
select id_code
from tablename
order by
substring_index(substring_index(id_code,'_',-2),'_',1),substring_index(id_code,'_',-1)
You can try in MYSQL to split your string, and order by parts :
SELECT SUBSTRING_INDEX(ID_DATA, '_', 1) as part1,
SUBSTRING_INDEX(SUBSTRING_INDEX(ID_DATA, '_', 2), '_', -1) as part2,
SUBSTRING_INDEX(ID_DATA, '_', -1) as part3,
FROM table
ORDER BY part1, part2, part3