Forum Moderators: open

Message Too Old, No Replies

Custom sort

         

Nutter

4:23 pm on May 14, 2006 (gmt 0)

10+ Year Member



I'd like to do a custom sort on an enum field in MySQL. The field is 'color', 'bw', 'sepia' and I'd like to be able to sort them on that field in that order. Would this be possible?

This is the same order I have them entered in the enum field. Is there a way to sort on the underlying index? My understanding is that the data is actually stored as a numeric value.

coopster

6:03 pm on May 15, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You should be fine as is since the default sort order is the index.


ENUM
values are sorted according to the order in which the enumeration members were listed in the column specification. (In other words,
ENUM
values are sorted according to their index numbers.) For example,
'a'
sorts before
'b'
for
ENUM('a', 'b')
, but
'b'
sorts before
'a'
for
ENUM('b', 'a')
. The empty string sorts before non-empty strings, and
NULL
values sort before all other enumeration values. To prevent unexpected results, specify the
ENUM
list in alphabetical order. You can also use
GROUP BY CAST(col AS CHAR)
or
GROUP BY CONCAT(col)
to make sure that the column is sorted lexically rather than by index number.

Resource:
[dev.mysql.com...]

Nutter

1:35 am on May 16, 2006 (gmt 0)

10+ Year Member



So I don't sort on that field at all and it'll come out correct? Sounds too easy :-)