php - Alphanumeric sorting/ordering from database -
in database, store values of a1, a2, a10, a12, a5, a8 when retrieve out show below:-
a1, a10, a12, a2, a5, a8
is there way sort correctly? like
a1, a2, a5, a8, a10, a12
if string starts single alpha/numeric prefix can use
select no my_table order substring(no,1,1), substring(no,2)+0
in php can sort once it's in multidimensional array using usort returning results of strnatcasecmp call on fields.
usort($records, function($a, $b){return strnatcasecmp($a["fieldname"], $b["fieldname"]);});
Comments
Post a Comment