Sort MySQL query result by a alphanumeric field

Posted by Jason Shultz on Stack Overflow See other posts from Stack Overflow or by Jason Shultz
Published on 2012-04-12T04:10:33Z Indexed on 2012/04/12 5:29 UTC
Read the original article Hit count: 135

Filed under:
|

I'm querying a table in a db using php. one of the fields is a column called "rank" and has data like the following:

none
1-bronze
2-silver
3-gold
...
10-ambassador
11-president

I want to be able to sort the results based on that "rank" column. any results where the field is "none" get excluded, so those don't factor in. As you can already guess, right now the results are coming back like this:

1-bronze
10-ambassador
11-president
2-silver
3-gold

Of course, I would like for it to be sorted so it is like the following:

1-bronze
2-silver
3-gold
...
10-ambassador
11-president

Right now the query is being returned as an object. I've tried different sort options like natsort, sort, array_multisort but haven't got it to work the way I'm sure it can. I would prefer keeping the results in an object form if possible. I'm passing the data on to a view in the next step. although, it's perfectly acceptable to pass the object to the view and then do the work there. so it's not an issue after all. :)

thank you for your help. i'm hoping I'm making sense.

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql