I have a list of data from SQL field. I want to sort by the field ASC but when i do it comes up in the wrong order. I know why it does it, but wondered if there was a solution around this problem. Ive heard of natsort php function, but not investigated it. Is there an easy way?
Academy
Under 10′s Blue
Under 10′s Green
Under 11′s Red
Under 11′s White
Under 13′s Blue
Under 13′s Red
Under 13′s White
Under 14′s Blue
Under 15′s Blue
Under 15′s Red
Under 15′s White
Under 16′s Red
Under 18′s Blue
Under 18′s Red
Under 7′s
Under 8′s Red
Under 9′s Red
There is a very simple approach to sort this list. For all values of
Academystarting withUnderyou perform the sort algorithm based on the following ORDER BY clause:The first sort column is based on removing the string ‘Under ‘ and then adding 0. This will force an ordering of the resulting integer.
Here is an example of computing the numeric value by removing ‘Under ‘ first:
The second sort column will order by the string value of Academy. All
'Under 15's'are grouped together and alphanumerically sorted.Here is your sample data from the question loaded into a table and sorted:
Give it a Try !!!